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.
asked Aug 13, 2011 at 14:54
Saket Choudhary
6242 gold badges11 silver badges25 bronze badges
-
3Why a regex? Just string-replace " check" with " someothercheck" and "(" with "\(" and ")" with "\)".Kerrek SB– Kerrek SB2011年08月13日 14:57:26 +00:00Commented Aug 13, 2011 at 14:57
-
1like mystring.replace('check','somothercheck').replace('(','\\(').replace(')','\\)'); jsfiddle.net/ytnRxstecb– stecb2011年08月13日 15:00:36 +00:00Commented Aug 13, 2011 at 15:00
-
3 regexes are better than onePablo Fernandez– Pablo Fernandez2011年08月13日 15:02:31 +00:00Commented Aug 13, 2011 at 15:02
-
I have edited the question . Please see that paranthses are present elsewhere and @stebs method won't work.Saket Choudhary– Saket Choudhary2011年08月13日 15:41:36 +00:00Commented Aug 13, 2011 at 15:41
2 Answers 2
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();"
answered Aug 13, 2011 at 14:58
user278064
10.2k1 gold badge36 silver badges48 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Pablo Fernandez
Don't take him literally. Consider
someothercheck as an exampleSaket Choudhary
I have edited the question.There are other parantheses in the string too.
aalku
@Pablo, Don't take him (@user278064) literally. Consider the answer as an example.
Saket Choudhary
@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)"
aalku
It wasn't me, it was @user278064
var myString = "abcdef; check(x,y); cef;";
myString.replace(/(\w+)\(/, 'someother1ドル(')
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)')
answered Aug 13, 2011 at 15:00
Pablo Fernandez
106k60 gold badges196 silver badges234 bronze badges
1 Comment
Saket Choudhary
I have edited the question. We need to take into account that there are other parantheses in the string too.
lang-js