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

标签 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.'
  ],
];

处理它的最佳方法是什么?我尝试过正则表达式,但无法正确使用。

最佳答案

我认为你可以使用:

(\w)'(\w)

DEMO

匹配被字母包围的撇号,然后使用捕获的组 ($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/

相关文章:

Javascript删除codeigniter中的确认框

javascript - ngRoute 和 Underscore.js 的 RequireJS shim 配置

javascript - 如何使用VueJS动态获取按钮的值

ruby - 正则表达式在 Rails Ruby 的字符串中查找键?

javascript - 从 javascript 文件中去除注释

php - RegEx 和 MySQL 基础知识 - 替换字符串的不同部分

使用 < 和 > 时的 JavaScript 正则表达式问题

com - 访问 COM 对象中的枚举 [jscript]

javascript - 从 jQuery 对象中移除元素

regex - 如何执行否定环视搜索