This might be easy for those who play with regular expressions.
对于那些使用正则表达式的人来说,这可能很容易。
str = "Here is 'sample' test";
str = str .replace(new RegExp('"', 'g'), '');
str = str .replace(new RegExp("'", 'g'), '');
How to combine 2nd and 3rd line, I want to combine regular expressions new RegExp('"', 'g')
and new RegExp("'", 'g')
into one regular expression, this will make it in one line. Thanks in advance.
如何组合第2行和第3行,我想将正则表达式新的RegExp('“','g')和新的RegExp(”'“,'g')组合成一个正则表达式,这将使它在一行中。提前致谢。
6 个解决方案
#1
2
Try:
str = str.replace(new RegExp('["\']', 'g'), '');
#2
8
str = str.replace(/"|'/g, '')
#3
3
str.replace(/['"]+/g, '')
#4
0
You can simply use a character class for this, to match both single and double quotes you can use ['"]
.
你可以简单地使用一个字符类来匹配你可以使用['“]的单引号和双引号。
In a full regex you would need to escape one of the quotes though.
在一个完整的正则表达式,你需要逃脱其中一个引号。
var str = "here is 'sample' test";
str = str.replace(/["']/g, '');
#5
0
Similar to Andrew's solution:
与Andrew的解决方案类似:
str.replace(/"|'/, 'g')
And if you seeking for a good explanation then this has been discussed on a different threat where Alan Moore explains good. Read here.
如果你寻求一个好的解释,那么就已经讨论过艾伦摩尔解释良好的另一个威胁。在这里阅读
#6
-1
str = "Here is 'sample' test".replace(new RegExp('"', 'g'), '').replace(new RegExp("'", 'g'), '');
An example that's basically the same as yours, except it uses method chaining.
一个与你的基本相同的例子,除了它使用方法链。
#1
2
Try:
str = str.replace(new RegExp('["\']', 'g'), '');
#2
8
str = str.replace(/"|'/g, '')
#3
3
str.replace(/['"]+/g, '')
#4
0
You can simply use a character class for this, to match both single and double quotes you can use ['"]
.
你可以简单地使用一个字符类来匹配你可以使用['“]的单引号和双引号。
In a full regex you would need to escape one of the quotes though.
在一个完整的正则表达式,你需要逃脱其中一个引号。
var str = "here is 'sample' test";
str = str.replace(/["']/g, '');
#5
0
Similar to Andrew's solution:
与Andrew的解决方案类似:
str.replace(/"|'/, 'g')
And if you seeking for a good explanation then this has been discussed on a different threat where Alan Moore explains good. Read here.
如果你寻求一个好的解释,那么就已经讨论过艾伦摩尔解释良好的另一个威胁。在这里阅读
#6
-1
str = "Here is 'sample' test".replace(new RegExp('"', 'g'), '').replace(new RegExp("'", 'g'), '');
An example that's basically the same as yours, except it uses method chaining.
一个与你的基本相同的例子,除了它使用方法链。