Solved

Javascript and regular expression : parenthesis in a pattern

Posted on 2004-03-25
5
435 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 100 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

763 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