简体   繁体   English

JavaScript 正则表达式从字符串中删除内容

[英]JavaScript Regex Remove Content from String

I have a string like this:我有一个这样的字符串:

"cghdfgf' hgfhdfgh 'hgfhf'hghdf'gf' 'ghfdh"

Using a regex ( by replacing a regex with a string, eg str.replace(/regex/g, '') ) I want to be able to strip all characters between single quotes within that string, so that it looks like this:使用正则表达式(通过用字符串替换正则表达式,例如str.replace(/regex/g, '') )我希望能够去除该字符串中单引号之间的所有字符,使其看起来像这样:

"cghdfgf''hgfhf''gf''ghfdh"

Is this possible with a regex?这可以用正则表达式吗?

Ya, you may try this.是的,你可以试试这个。

string.replace(/'[^']*'/g, "''")

[^']* negated character class which matches any character but not of single quote, zero or more times. [^']*否定字符类,匹配任何字符但不匹配单引号,零次或多次。

you may use你可以使用

string.replace(/'.*?'/g,"''");

hope it helps!希望能帮助到你!

below is the snippet下面是片段

 var test="cghdfgf' hgfhdfgh 'hgfhf'hghdf'gf' 'ghfdh"; var ganti=test.replace(/'.*?'/g,"''"); alert(ganti);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM