我想从字符串(在javascript中)替换大多数特殊字符,但允许一些特殊情况,如c ++,c#等。我在node.js中尝试过xregexp库,我想能够删除所有非字母和数字。我还想允许所有外语字母。这就是我到目前为止所做的:
var str = "I do programming in c++ and sometimes c#, but + and # should be removed";
regex = XRegExp('[^\\s\\p{N}\\p{L}]+', 'g');
var replaced = XRegExp.replace(str, regex, "");
console.log(replaced);
此输出
I do programming in c and sometimes c, but and should be removed
我需要用允许的单词创建某种列表,比如c ++和c#。期望的输出是:
I do programming in c++ and sometimes c#, but and should be removed
答案 0 :(得分:1)
您可以在捕获组中使用替换,然后在替换模式中使用反向引用恢复此文本:
var str = "I do programming in c++ and sometimes c#, but + and # should be removed";
regex = XRegExp('(\\b(?:c[+]{2}|c#)(?!\\w))|[^\\s\\p{N}\\p{L}]+', 'ig');
// ^-- capture group 1 -----^ ^
var replaced = XRegExp.replace(str, regex, "$1");
// ^^
console.log(replaced);
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/2.0.0/xregexp-all-min.js"></script>
注意我添加了i
标志以使模式不区分大小写,\b
在替换开始时仅匹配单词边界(自c++
和{{1}以来}以字母(单词字符)和前瞻c#
开头,确保(?!\w)
和+
之后没有单词字符#
\b
在这里不起作用不是单词字符。)