我想过滤给定的字符串并替换给定字符串中的一些常用词:
const str ='api/knwl/tests/products';
const str1 = 'api/users';
const str2 = 'api/tests/providers';
我想从给定的字符串中过滤单词“api”、“knwl”或“tests”。
我尝试了一些正则表达式:
/(?!(tests|api|knwl))/g
但它不起作用。我该如何解决这个问题?我不是正则表达式方面的专家。
最佳答案
正则表达式 - /(tests|api|knwl)/g
;
const str ='api/knwl/tests/products';
const str1 = 'api/users';
const str2 = 'api/tests/providers';
const filterRegex = str.replace(/(tests|api|knwl)/g,''); // replace all occurence of strings (tests,api,knwl) to empty.
关于javascript - 如何在 JavaScript 中使用正则表达式替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53122167/