Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript and regular expression : parenthesis in a pattern

Posted on 2004-03-25
5
Medium Priority
?
444 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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 discusses how to create an extensible mechanism for linked drop downs.
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…

664 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