Topic: JavaScript Regex Issue Pages that link to <a href="https://ozoneasylum.com/backlink?for=29317" title="Pages that link to Topic: JavaScript Regex Issue" rel="nofollow" >Topic: JavaScript Regex Issue\

 
Author Thread
aaroncampbell
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Jun 2007

IP logged posted posted 06-29-2007 02:22 Edit Quote

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');


My problem is that whatever new_vin is, the regex will match it on a line by itself, but NOT if there is a space before or after it. shouldn't the [\s]* allow the spaces?

(Edited by aaroncampbell on 06-29-2007 02:24)

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

IP logged posted posted 06-29-2007 03:02 Edit Quote

First of all, hello and welcome to the Asylum.

You should escape the value of the tags before injecting them into a RegExp. Or, another, faster in theory, method is to not use any Regexp, i.e:

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,



(Edited by poi on 06-29-2007 03:03)

aaroncampbell
Obsessive-Compulsive (I) Inmate

From:
Insane since: Jun 2007

IP logged posted posted 06-29-2007 03:59 Edit Quote

I'm sorry, I don't know what you mean by "tags" Do I need to escape something that's not escaped?
As for your way, as far as I can tell, it would only work if there was no other text. What if the input of the textarea was:
-----------------------------------------------------
This is some text with a __17_digit_VIN___
More text, ___another_VIN___
_replace_this_VIN
_replace_this_VIN
_replace_this_VIN
-----------------------------------------------------

Basically, there are 2 things that I'm doing. First, I'm replacing any 17+ character strings with a valid VIN, IF they contain one (stupid barcodes on vehicles often have extraneous characters...). Then (and this is the part I can't seem to get), *IF* a VIN is all by itself on a line (whitespace ignored), I want to replace it with: year make model style VIN weight

aaroncampbell
Obsessive-Compulsive (I) Inmate

From:
Insane since: Jun 2007

IP logged posted posted 06-29-2007 06:54 Edit Quote

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:
var vin_only = new RegExp("^\\s*"+new_vin+"\\s*$", 'm');

Thanks again.

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

IP logged posted posted 06-29-2007 09:02 Edit Quote

Oh, didn't spot the \\s one, but hey it was 3am.

What I meant is that in absolute new_vin could include control characters ( e.g. []()+*?^$\. ) that will break your regExp. I said that because you gave no information about the possible values of old_vin and new_vin ... and I'm still unsure what they can be.

aaroncampbell
Obsessive-Compulsive (I) Inmate

From:
Insane since: Jun 2007

IP logged posted posted 06-29-2007 16:51 Edit Quote

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])

Thanks



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu