是否可以更改此设置:
Hello, this is Mike [example]
Run Code Online (Sandbox Code Playgroud)
对此:
Hello, this is Mike
Run Code Online (Sandbox Code Playgroud)
使用JS + Regex?谢谢。
如果您更喜欢使用正则表达式执行此操作,请考虑以下内容。
var r = 'Hello, this is Mike [example]'.replace(/ *\[[^\]]*]/, '');
console.log(r); //=> "Hello, this is Mike"
Run Code Online (Sandbox Code Playgroud)
如果您的数据在要删除的方括号中包含更多文本,请使用g(全局)修饰符。
根据给定的字符串,您可以在此处使用split。
var r = 'Hello, this is Mike [example]'.split(' [')[0]
console.log(r); //=> "Hello, this is Mike"
Run Code Online (Sandbox Code Playgroud)