Regular Expressions and Substitution

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!
 
kls1Asked:
Who is Participating?
 
ZvonkoConnect With a Mentor Systems architectCommented:
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
 
ZvonkoSystems architectCommented:
Show the source text string and show the resulting text string and I can show you the RegExp for doing it.
0
 
ZvonkoSystems architectCommented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
ZvonkoSystems architectCommented:
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
 
kls1Author Commented:
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
 
chippleConnect With a Mentor Commented:
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
 
kls1Author Commented:
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
 
kls1Author Commented:
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
 
chippleCommented:
I don't care so much for points, I'm glad if it helped. :)

Good luck!
0
 
ZvonkoSystems architectCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.