Javascript Regular Expressions
Jan.14, 2008 in
Tech
I was attempting to complete a very simply javascript regular expression which removes a few characters from a string:
my_field_124 => my_field
So i created the following javascript:
fieldName.replace (/\_\d+/, '');
alert(fieldName);
What I thought would display is the string “my_field”. Instead I kept getting “my_field_1″ returned. The reason for this is that the replace() function returns the resulting value, and doesn’t change the value of the original string! Change the script to:
alert(fieldName.replace(/\_\d+, '');
And it works!

Leave a Reply