Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何编写正则表达式来匹配 javascript 中的“暗”或“亮”。它将被替换为完整的单词。
尝试这个:
str.match(/\b(dark|light)\b/g)
演示:http: //jsfiddle.net/zhggQ/
/\b(?:dark|light)\b/g
只匹配整个单词。看一下
replace
尝试这个
"暗或亮".replace(/(dark|light)/g,"other")
/(\sdark\s|\slight\s)/ 您需要在单词周围提供可选的前导或尾随空格