Why is \W meta character not working properly in this Javascript regex -
hello i'm having trouble understanding regex.
'^.*(?=.{8,})(?=.*[a-z])(?=.*[a-z])(?=.*[\d\w]).*$' it should match zxc!"#zxc123, in online js regex tester, when try in application working on, wont match that. seems require letter w, assume comes non-word selector \w.
so case is, works in online tester, not actual js.
i've set example on fiddle, can see yourself.
$(document).ready(function(){ var inputfield = $('#regex'); inputfield.on('input', function(e){ var inputvalue = $(e.currenttarget).val(); matchstring(inputvalue); }); function matchstring(str){ var matches = str.match('^.*(?=.{8,})(?=.*[a-z])(?=.*[a-z])(?=.*[\d\w]).*$'); if(matches){ $('#output').text('matches') } else{ $('#output').text('not matching') } } matchstring(inputfield.val()); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="regex" value='zxc!"#zxc!"#'/> <p id="output"></p> i've tested regex on regex101
i hope explanation, because seems odd me. in advance
it because syntax not right regex. in js regex should wrapped /.../ / called regex delimiter.
also regex can refactored efficiency this:
/^(?=.*?[a-z])(?=.*?[a-z])(?=.*?[\d\w]).{8,}$/
Comments
Post a Comment