我在正则表达式后的末尾附加了一个未定义的字符串,例如:
var string="test.testA:(number:'1')undefined'
有时它会附加到字符串上
var string2 = "test.testA:(number:'1') and undefined"
基本上这可以在任何地方,我想使用正则表达式删除它。正则表达式是否检查字符串中是否存在未定义?如果不是,那么删除字符串中存在的“未定义”测试的最佳解决方案是什么?
谢谢!
最佳答案
// remove only "undefined"
var test = [
"test.testA:(number:'1')undefined",
"test.testA:(number:'1') and undefined",
];
console.log(test.map(function (a) {
return a.replace(/\bundefined\b/, '');
}));
// remove the whole line
var test2 = [
"test.testA:(number:'1')undefined",
"test.testA:(number:'1') and undefined",
];
console.log(test2.map(function (a) {
return a.replace(/^.*?\bundefined\b.*$/, '');
}));
关于javascript - 如何使用 regex-Javascript 检查字符串中是否存在 "undefined"测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52010203/