javascript - Chrome changes regex and can't match with value from dropdownlist -


i'm having problems regex , can't value validated.

i have function this:

function validationobject(validationname, validationregex) {      this.validationname = validationname;      this.validationregex = validationregex; } 

it creates validationobject parameters gets. dropdownlist containing number have regex:

\d+ 

but when validate value regex piece of code won't work:

if (inputvalue.match(validatingregex)) {      dosomethings(); }  

if check validationobject see chrome has changed regex /d+/ . i've tried setting regex-type, doesn't work either. work on textfields. seems me backslashes converted else.

also i've tried escaping backslash browser takes literal value.

hopefully have answer, help!

best regards,

boyyerd

you need escape special chars if you're passing them regexp constructor:

var expr = new regexp(somstr.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), 'i'); 

since you're passing \d+, backsash needs escaping, should possing \\d+ constructor.
regex above escapes special chars afaik. i've taken liberty of copying expression from here.
alternative, , easier way create expression use literal notation, far more common, more in tune js, , less error-prone:

var expr = /\d+/; 

no need escape string @ all.

you can check in console btw: '\d' evaluates "d" in chrome console, passing '\d+' regexp evaluates new regexp('d+'), valid regex, no errors thrown, doesn't same thing.

a side-note, though: clarify goal of function is, exactly, because it's no constructor (if is, name should start uppercase letter), , don't think understand this reference can/will in case


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -