Topic: JavaScript Regex Issue |
|
---|---|
Author | Thread |
Neurotic (0) Inmate Newly admitted From: |
posted 06-29-2007 02:22
First, the code: code: /* Ok, I'm using prototypejs, so $(el) is a reference to a textarea, and $F(el) is the value of that textarea */ var old_vin = get_text(vn.getElementsByTagName('old').item(0)); var new_vin = get_text(vn.getElementsByTagName('new').item(0)); $(el).value = $F(el).replace(old_vin, new_vin); var vin_only = new RegExp("^[\s]*"+new_vin+"[\s]*$", 'm'); console.log(vin_only); console.log('Only VIN('+new_vin+') on line: ', $F(el).match(vin_only)); $(el).value = $F(el).replace(vin_only, 'test');
|
Paranoid (IV) Inmate From: Norway |
posted 06-29-2007 03:02
First of all, hello and welcome to the Asylum. code: var old_vin = get_text(vn.getElementsByTagName('old').item(0)) ,new_vin = get_text(vn.getElementsByTagName('new').item(0)) ,new_value = $F(el).split( old_vin ).join( new_vin ); if( new_vin==new_value.replace( /^\s+|\s+$/g, '' ) ) new_value = 'test'; $(el).value = new_value; Hope that helps, |
Obsessive-Compulsive (I) Inmate From: |
posted 06-29-2007 03:59
I'm sorry, I don't know what you mean by "tags" Do I need to escape something that's not escaped? |
Obsessive-Compulsive (I) Inmate From: |
posted 06-29-2007 06:54
Thanks, I actually figured out what you meant. when using the new RegExp method, you have to double escape the special characters. So, to do a \s (whitespace), you actually have to do \\s. This works great: |
Paranoid (IV) Inmate From: Norway |
posted 06-29-2007 09:02
Oh, didn't spot the \\s one, but hey it was 3am. |
Obsessive-Compulsive (I) Inmate From: |
posted 06-29-2007 16:51
Sorry, old_vin is limited to alpha numeric, and new_vin is limited to \d, A-H, J-N, P, & R-Z ([\dA-HJ-NPR-Z]) |