首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用regex替换撇号,除非用逗号分隔。

用regex替换撇号,除非用逗号分隔。
EN

Stack Overflow用户
提问于 2015-07-23 15:50:22
回答 1查看 207关注 0票数 2

我有一个字符串,需要使用javascript来清理。

下面是一个给我下地狱的字符串的例子。我需要替换所有没有用逗号分隔的' (比如我的示例中的"apostrophe's“)。

下面是一个例子:

代码语言: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.'
  ],
];

什么是最好的方法来接近它?我试过regex,但没能把它做好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-23 16:49:10

我想你可以用:

代码语言:javascript
复制
(\w)'(\w)

演示

若要匹配由字母包围的撇号,然后使用捕获的组( e's )替换例如es,如下所示:

代码语言:javascript
复制
var str = "'this is something with apostrophe's'";
var res = str.replace(/(\w)'(\w)/g, "$1$2");

其结果是:

“这是带有撇号的东西”

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31592309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档