需要帮助查找javascript代码
问题描述:
我正面临着这个问题。我得到这样的字符串。需要帮助查找javascript代码
'=--satya','=---satya1','=-----satya2'.
现在我的问题是,我不得不删除这些特殊字符,打印字符串这样
'satya'
'satya1'
'satya2'
请帮忙解决这个问题?
答
使用JavaScript函数replace它可以帮助你用正则表达式这种情况下
var string = '=---satya1';
string = string.replace(/[^a-zA-Z0-9]/g, '');
答
您可以提取使用正则表达式,如
/\'\=-{0,}(satya[0-9]{0,})\'/
活生生的例子信息:http://jsfiddle.net/LFZje/
正则表达式匹配
字面'
字面=
零个或多个-
启动一个捕获团的捕捉
- 字面satya
- 零个或多个numbers
完使用代码捕获组
字面'
然后如
var regex = /\'\=-{0,}(satya[0-9]{0,})\'/g;
while((match = regex.exec("'=--satya','=---satya1','=-----satya2'")) !== null)
{
// here match[0] is the entire capture
// and match[1] is tthe content of the capture group, ie "satya1" or "satya2"
}
查看实时示例的更多详细信息。
答
var s = '=---satya1';
s.replace(/[^a-zA-Z0-9]/g, '');
更换所有非字母和非数字字符或
s.replace(/[-=]/g, '');
删除所有-
和=
人物,甚至
'=---satya-1=test'.replace(/(=\-+)/g, ''); // out: "satya-1=test"
到防止进一步删除-
或=
。
这样的事情: str.replace(/(= | - )* /,“”) –