我有一个字符串需要使用 javascript 进行清理。
下面是一个让我很头疼的字符串示例。我需要替换所有未用逗号分隔的 '
(例如我的示例中的“撇号”)。
例子如下:
var paItems = [
[
38.20739,
-85.76427,
1,
'asd',
'314 Iowa Ave, Louisville, KY',
'this is something with apostrophe's',
'2000',
'2',
'2',
'1',
'Yes',
'',
'',
'Area Tennis|Satellite Dish|Controlled Access',
'asd',
'asd',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
''
],
[
38.20681,
-85.71437,
2,
'somewhere',
'3634 Poplar Level Rd, KY ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard ever since the 1500s, ',
'1200',
'3',
'2',
'1',
'Yes',
'Garage',
'2250',
'Private Pool|Area Pool|Satellite Dish',
'2',
'Some subdivision here',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
'Glenda C.'
],
];
处理它的最佳方法是什么?我尝试过正则表达式,但无法正确使用。
最佳答案
我认为你可以使用:
(\w)'(\w)
匹配被字母包围的撇号,然后使用捕获的组 ($1$2
) 将例如 e's
替换为 es
,如:
var str = "'this is something with apostrophe's'";
var res = str.replace(/(\w)'(\w)/g, "$1$2");
结果:
'this is something with apostrophes'
关于javascript - 用正则表达式替换撇号,除非用逗号分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31592309/