javascript - 删除特定字符并替换为单词

标签 javascript

如果我有这个字符串:

This string should @[1234] by another word

我想替换以删除 @ [ ] 并将 1234 替换为“test”一词,例如,结果是:

This string should test by another word

有没有办法用js来实现这个功能?

最佳答案

您可以使用正则表达式替换您的@[XXX],通过以下代码进行测试:

var string = "This string should @[1234] by another word";
console.log(string.replace(/@\[[0-9]+\]/gi, "test"));

关于javascript - 删除特定字符并替换为单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48147912/

相关文章:

javascript - Angular 教程 - 我在哪里迷路了?

javascript - react : Avoiding Re-rendering Same Component on Routing

javascript - react : why does the onclick get executed even without having clicked?

javascript - 检查小数点前 4 个数字和小数点后 1 个数字

javascript - 如何将 id 传递给 getElementById() 方法?

javascript - 替换字符串中的单词,但忽略 HTML

javascript - 如何使用 Moment.js 将日期验证为指定格式?

javascript - ng.core.Component(...).Class 不是函数

javascript - 如何使用aes-128-cbc算法实现CryptoJS解密?

javascript - 如何在满足特定条件时删除 Vue 2 中的 eventListener (window.removeEventListener)