0

There are some strings

"string numbe1 fdsf gfdgfdgf (value1)"
"string numbe2 fdsf gfdgfdgf (value2)"
"string numbe3 fdsf gfdgfdgf (value3)"
"string numbe5 fdsf gfdgfdgf (value1)"
"string numbe5 fdsf gfdgfdgf (value2)"

(valueN) can be either (value1) or (value2) or (value3). I need to cut it from the original string by using javascript regex. So the newString must be kind of string numbe1 fdsf gfdgfdgf

Here is what I did (and which is not working as I want)

var newString = originalString.replace(/(value1)|(value2)|(value3)/g,"") ;

It returns "1 ()".

How can I solve it?

6 Answers 6

1

you need to escape the parenthesis, and you have an extra . right before replace

"string numbe5 fdsf gfdgfdgf (value2)".replace(/\(value[1-3]\)/, '')
Sign up to request clarification or add additional context in comments.

Comments

0

Try using

var newString = originalString.replace(/\(value\d\)/g,"") ;

Comments

0

i'd use this regular expression:

"string numbe5 fdsf gfdgfdgf (value2)".replace(/ \(value[123]\)/, '')

Comments

0

Not exactly extensible, but if your string formats are always the same you could just do this:

var str = "string numbe1 fdsf gfdgfdgf (value1)";
str = str.slice(0, str.length-8));​

Comments

0

Try the following:

> var s = "string numbe1 fdsf gfdgfdgf (value1)";
> s.replace(/^(.*) \(value[1-3]\)$/, '$1');
"string numbe1 fdsf gfdgfdgf"

Comments

0

There is a mistake in the code, Use the code given below

<script>
function myFunction()
{
var str="string numbe1 fdsf gfdgfdgf (value2)"; 
var n=str.replace(/(\(value1\)|\(value2\)|\(value3\))/g," ");
alert(n);
}
</script>

Comments

Your Answer

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