Solved

Javascript and regular expression : parenthesis in a pattern

Posted on 2004-03-25
5
423 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
Comment Utility
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
Comment Utility
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
Comment Utility
try this

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

alert(html)
</script>
0
 
LVL 1

Author Comment

by:Cyrrus30
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now