我想替换字符串中的大多数特殊字符(在 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