Removing a portion of text

I need to remove all the portion of text in square braces along with the braces, in TypeScript. How can I do this?

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

Upvotes: 0

Views: 63

Answers (3)

Siddhartha Chowdhury
Siddhartha Chowdhury

Reputation: 2732

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

Thanks

Upvotes: 1

Gabriele Ciech
Gabriele Ciech

Reputation: 7975

You can use the javascript function 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 " 

Upvotes: 1

bthe0
bthe0

Reputation: 1424

You may use a RegExp in conjucation with the replace function. Like this:

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

Upvotes: 0

Related Questions