是否可以更改此设置:

Hello, this is Mike [example]

对此:
Hello, this is Mike

使用JS + Regex?谢谢。

最佳答案

如果您更喜欢使用正则表达式执行此操作,请考虑以下内容。

var r = 'Hello, this is Mike [example]'.replace(/ *\[[^\]]*]/, '');
console.log(r); //=> "Hello, this is Mike"

如果您的数据在要删除的方括号中包含更多文本,请使用g(全局)修饰符。

根据给定的字符串,您可以在此处使用split。
var r = 'Hello, this is Mike [example]'.split(' [')[0]
console.log(r); //=> "Hello, this is Mike"

关于javascript - Javascript/regex:删除方括号之间的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25776973/

10-13 09:13