0

I have the following string in JavaScript

  var mystring = " abcdef(p,q); check(x,y); cef(m,n);"

I would want to do a string replace such that my final string is :

  mystring = " abcdef(p,q); someothercheck\(x,y\); cef(m,n);"

x and y should remain same after the substitution. and the backslashes are necessary since I need to pass them to some other command.

There can be other Parantheses in the string too.

4
  • 3
    Why a regex? Just string-replace " check" with " someothercheck" and "(" with "\(" and ")" with "\)". Commented Aug 13, 2011 at 14:57
  • 1
    like mystring.replace('check','somothercheck').replace('(','\\(').replace(')','\\)'); jsfiddle.net/ytnRx Commented Aug 13, 2011 at 15:00
  • 3 regexes are better than one Commented Aug 13, 2011 at 15:02
  • I have edited the question . Please see that paranthses are present elsewhere and @stebs method won't work. Commented Aug 13, 2011 at 15:41

2 Answers 2

4

If you don't have other parenthesis, it should be easy.

mystring = mystring.replace("check(", "someothercheck\\(");
mystring = mystring.replace(")", "\\)");

EDIT This works also in the case of multiple parenthesis (It does not affect the empty ones). var str=" abcdef; check(x,y); cef();" patt = /((\w)/g;

// transform (x in \(x
str = str.replace(patt, '\\($1');

patt = /(\w)\)/g

// transform y) in y\);
str = str.replace(patt,  '$1\\)');

// transform check in someothercheck
str = str.replace('check', 'someothercheck');

EDIT Now it converts only the check strings.

function convertCheck(str, check, someOtherCheck) {
   // console.log(str + " contains " + check + "? ");
   // console.log(str.indexOf(check));

   if (str.indexOf(check) === -1) return str;
   var patt1 = /\((\w)/g,
   patt2 = /(\w)\)/g; 

   str = str.replace(patt1, '\\($1');
   str = str.replace(patt2,  '$1\\)');
   str = str.replace(check, someOtherCheck);

   return str;
}

var str = "abcdef(); check(x,y); cef();",
    tokens = str.split(';');
for (var i = 0; i < tokens.length; i++) {
   tokens[i] = convertCheck(tokens[i], "check", "someothercheck");
}
str = tokens.join(";");

alert(str);  // "abcdef(); someothercheck/(x,y/); cef();"
Sign up to request clarification or add additional context in comments.

5 Comments

Don't take him literally. Consider someothercheck as an example
I have edited the question.There are other parantheses in the string too.
@Pablo, Don't take him (@user278064) literally. Consider the answer as an example.
@user270349 I am sorry my question wasn't clear ! I need to check it with the string : "func1(a,b,c); func2(a,v,b); check (x,y); cef(c,d)"
It wasn't me, it was @user278064
2
var myString = "abcdef; check(x,y); cef;";

myString.replace(/(\w+)\(/, 'someother$1(')
        .replace(/\(/g, '\\(')
        .replace(/\)/g, '\\)')

1 Comment

I have edited the question. We need to take into account that there are other parantheses in the string too.

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.