简体   繁体   English

删除部分文字

[英]Removing a portion of text

I need to remove all the portion of text in square braces along with the braces, in TypeScript. 我需要在TypeScript中删除方括号中所有文本部分。 How can I do this? 我怎样才能做到这一点?

var text="All-Weather Floor Liner Package [installed_msrp]"

You can use the javascript function string.replace 您可以使用javascript函数string.replace

var text="All-Weather Floor Liner Package [installed_msrp]"
var myCleanedStr = text.replace(/(\[.*\])/g, "");
console.log(myCleanedStr); // Output is "All-Weather Floor Liner Package " 

Try this, hope it helps 试试这个,希望对您有所帮助

var text="All-Weather Floor Liner Package [installed_msrp]"

alert(text.replace(/\s*\[.*?\]\s*/g, ''));

This will also remove excess whitespace before and after the parentheses 这还将删除括号前后的多余空格。

JSFIDDLE JSFIDDLE

Thanks 谢谢

You may use a RegExp in conjucation with the replace function. 您可以结合使用RegExp和replace函数。 Like this: 像这样:

'All-Weather Floor Liner Package [installed_msrp]'.replace(/\[.*\]/g, '');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM