[英]Javascript - Regex replace ignoring some special character
I have a string which can be.. 我有一个字符串可以..
Samsung Blu-Ray Player
Samgung Bluray player
Samsung Blu/Ray player
and I know want to find and replace bluray (later: to give it a certain color). 我知道想找到并替换蓝光(后来:给它一定的颜色)。 Therefore I want to replace the word using a regex. 因此,我想用正则表达式替换这个词。 But no idea about the regex syntax in that case. 但在这种情况下不知道正则表达式语法。 It should ignore "/" and " " and find the string, case insensitive. 它应该忽略“/”和“”并找到字符串,不区分大小写。
try this : 尝试这个 :
var str = "Samsung Blu-Ray Player";
var res = str.replace(/(Blu.*?ray)/gi, '<span style="color:red">$1</span>');
console.log(res);
You can replace the string using 您可以使用替换字符串
str = str.replace(/samsung blu[^\w]?ray player/gi, 'Samsung Blu Ray player')
where 哪里
[^\\w]?
- is any nonword character (non alphanumeric) - 是任何非字符(非字母数字) ?
的?
makes it optional 使它成为可选的 /gi
- the flags mean g
lobal search and case i
nsensitive /gi
-标志意味着g
叶形搜索和情况i
nsensitive /\\bblu\\s*[^\\w]?\\s*ray\\b/gi
\\b
the word boundary gives it more conservation from matching blurayfish
or so \\b
这个词边界使得它可以从匹配的blurayfish
更多的保护
\\s*
makes it white space insensitive so blu - ray
will match \\s*
使白色空间不敏感,因此blu - ray
将匹配
[^\\w]
matches non-word symbols like -
, ?
[^\\w]
匹配非字符号,如-
, ?
means optional 意味着可选
gi
means all matches, case insensitive. gi
表示所有匹配,不区分大小写。
here is a more illustrative page, with test cases and explanation alongside. 这是一个更具说明性的页面,旁边有测试用例和解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.