Solved

Regular Expressions and Substitution

Posted on 2004-09-02
10
202 Views
Last Modified: 2008-02-01
I want to take a string that contains multiple form components and replace the name descriptor to be the name followed by an expression. For example, I want to change the following part of every component description:

name= 'someName'

to

name='someName' + aNumber  

where aNumber is a variable (integer). I want this to work on at least the major browsers.

Two ways I've considered is if there is a way to update the descriptor of a form component or the use of regular expressions. I don't really care how, I just can't seem to resolve the problem. For instance, when using replace, how can I tell it to "append" to the regular expression item that it found?

The key issue I am having is determining a simple way to use the string the regular expression just found. I believe this is the regular expression I want:

/name='\w*'/   but replace only seems to take a specific string as the second argument. Basically I just need to know if there is some reference to the expression I just found. I've tried $0 and $1, both are null.

Thanks for any quick help!

 

Thanks for any help!
 
0
Comment
Question by:kls1
  • 5
  • 3
  • 2
10 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 11967331
Show the source text string and show the resulting text string and I can show you the RegExp for doing it.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11967359
And if you do not want to change a string, but only want to change the form element names on the page, then you need no RegExp.
By the way, changing the form input elements name is possible, but it is a bad idea.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11967417
To see the values of input fields someName1 to someName3 on the actual form do it like this:
<script>
function showValues(theBtn){
  theForm = theBtn.form;
  for(i=1;i<=3;i++){
     alert(theForm["someName"+i].value);
  }
}
</script>
<body>
<form>
  <input type=text name="someName1">
  <input type=text name="someName2">
  <input type=text name="someName3">
  <input type=button value="Show" onClick="showValues(this)">
</form>
</body>

0
 

Author Comment

by:kls1
ID: 11967587
An example text string would be something like:

String someString = "<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>"

This is generated using another language and being sent to Javascript. I need to "append" numbers to the names of the fields since I'm using dynamic html to automatically generate "copies" of the template. So, I without appending the numbers, I would end up with something that looks like:


some String = "<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>"

(I added the newlines for clarity)

There can be an infinite number of elements in the original string and there can be and infinite number of "copies". Therefore, I either need to be able to actually access the name property and change it or I need to use regular expressions to change it.

Thanks again for any help!
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 11967768
Like this:

<script>

someString = "<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>\
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>\
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>\
<input type=text name='someName'> <P> <input type=text name='anotherName'><P><input type=text name='aName'>"

var rowNum = 0;
alert(someString.replace(/someName|anotherName|aName/g, function(p1){if(p1=="someName")rowNum++;return p1+rowNum}));

</script>

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Assisted Solution

by:chipple
chipple earned 250 total points
ID: 11970472
Example:

var html = "<input name='test'>";
var rx = new RegExp("(name='\\w+)(')");
var number = "0";
html = htlm.replace(rx,"$1"+number+"$2"));

Good luck!
0
 

Author Comment

by:kls1
ID: 11973237
Thanks! I was able to take your suggestions and use regular expressions to figure out howt o get this to work. I appreciate it!
0
 

Author Comment

by:kls1
ID: 11973347
I'm going to split the points on this. I can create a dummy question if you think that your contribution is worth more than I alot.

Thanks for the help, I appreciate it.
0
 
LVL 4

Expert Comment

by:chipple
ID: 11973400
I don't care so much for points, I'm glad if it helped. :)

Good luck!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11973403
You are welcome.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

758 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

19 Experts available now in Live!

Get 1:1 Help Now