使用正则表达式替换撇号,除非用逗号分隔

时间:2015-07-23 15:50:23

标签: javascript regex

我有一个字符串,我需要使用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.'
  ],
];

接近它的最佳方法是什么?我已经尝试过正则表达式,但无法做到正确。

1 个答案:

答案 0 :(得分:1)

我认为你可以使用:

(\w)'(\w)

DEMO

匹配字母包围的撇号,然后使用捕获的组(e's)替换为es $1$2,例如:

var str = "'this is something with apostrophe's'";
var res = str.replace(/(\w)'(\w)/g, "$1$2");

结果:

  

'这是撇号'