javascript - 如何使用 regex-Javascript 检查字符串中是否存在 "undefined"测试

标签 javascript regex

我在正则表达式后的末尾附加了一个未定义的字符串,例如:

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/

相关文章:

r - 为什么 grepl 不在 grepl (".*", LETTERS, fixed=T) 中返回 true?

regex - Grep/Sed 在两个多行标签之间

javascript - 为什么这个正则表达式返回这样的数组

javascript - D3 : Setting selection transform attribute in a nicer way

Javascript getElementById() 适用于文本框,但不适用于单选按钮或下拉选择器

javascript - 鼠标悬停移动图像位置

javascript - 如何在 Javascript 中显示 Flash 版本

python - 满足某个关键字后匹配值的正则表达式

javascript - 如果选中复选框切换,则在切换 div 中创建一个必需的输入字段

php - 正则表达式 mysql 函数