0

I have a unusual instance where an application is generating a string of data but is adding a suffix code at the end of string surrounded by parentheses. I need to find the best javascript method to remove the parentheses and data that is in it. One key thing is this string can be different but will always have the parentheses around the specific data that needs to be removed.

Anything in the parenthesis.

Example 
STR-STOOL RAW (STR)

Needs to be 
STR-STOOL RAW 

3 Answers 3

1

How about:

/\(.*\)$/
  • \( and \) matches the parenthesis
  • .* matches any character
  • $ is necessary to meet at the end of the expression.

Online Demo

var result = "(STR) STR-STOOL RAW (STR)".replace(/\(.*\)$/g,""); //outputs (STR) STR-STOOL RAW

In the above example only the last (STR) is replaced

Sign up to request clarification or add additional context in comments.

3 Comments

The global flag makes no sense here.
totally agree let me remove it. Thanks for highlighting it
The global flag didn't work, I used the other method .replace(/ (.*)/,'') and this works great.
0
var str = "STR-STOOL RAW (STR)";
var newStr = str.replace(/ \(.*\)/,'');
console.log(newStr);

1 Comment

Thanks for the input, now I understand regex method better now with this example.
0

this worked for me specially if your line has existing parentheses
\([^\)]*\)$
Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.