Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript and regular expression : parenthesis in a pattern

Posted on 2004-03-25
5
Medium Priority
?
449 Views
Last Modified: 2006-11-17
Hi

I'm using Javascript and Regular Expression (through the regexp object) to dynamicaly modify a html page. I have a problem when I'm trying to use a pattern with parentheses:

I want this:
somestring(1)

to become:
somestring(2)

but parentheses have a proper use in regular expression.. here's my line:

html = html.replace(new RegExp('somestring(1)',"g"),'somestring(2)')

It just doesn't find the pattern. I tried using the backslash character to transform this special character in a literal character, but still nothing:

html = html.replace(new RegExp('somestring\(1\)',"g"),'somestring(2)')

It's drving me nuts!

Thanks for your time!
0
Comment
Question by:Cyrrus30
5 Comments
 
LVL 14

Expert Comment

by:ziffgone
ID: 10680810
Try removing the ' from around your something(1) and something(2)

html = html.replace(new RegExp(somestring(1),"g"),somestring(2))

Hope that works.
0
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 400 total points
ID: 10680836
Here is an example

<script language="JavaScript">
     html = "somestring(1);;;;;;;somestring(1)"
     html = html.replace(/somestring\(1\)/gi,'somestring(2)')
     alert(html);
</script>
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10680904
try this

<script>
html='12345somestring(1)wxyz'
html.replace(/somestring\(1\)/gi,'somestring(2)')

alert(html)
</script>
0
 
LVL 1

Author Comment

by:Cyrrus30
ID: 10681450
Ok, your technique works great venkateshwarr, but there's a little detail missing. Sorry I didn't mention it in my question.

The '1' I want to change into a '2' is in fact a variable (index in a for loop). I tried to add this to your solution, but I didn't find the way. I hope it's possible!

So if you have an idea, tell me. And if you don't, I'll give the point to venkateshwarr as he answered my question.
0
 
LVL 1

Author Comment

by:Cyrrus30
ID: 10688309
Ok, I finaly find a way to insert a variable in my pattern. Here's what I did:

html = html.replace(new RegExp('somestring\\(' + I + '\\)',"g"),'somestring(2)')

It took my a while to figure otu how to concatenate using patterns. In the exemple you showed, the string where bounded by slashes to show that it was a pattern. But concatenating with that kind of string doesn't work quite well. So I had to use a normal string, but with this technique my backslashes used to diseapeared, until I tried the double backslashes. So the string:
'somestring\\(' + I + '\\)'
is in fact this (when I = 1):
somestring\(1\)
wich is exactly what you told me to use, so it's working!

Thanks again for you help!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question