regex - Matching non-numbers after a few characters with javascript -
i'm new regexes , i'm validating form. want use string.prototype.match filter out array responses incorrect. input should match form:
"foo-1234567" where each identifier starts foo- , has 7 digits. if there's more 1 identifier, input takes form:
"foo-1234567\nfoo-7654321\nfoo-1324536"` where identifiers separated linefeeds.
i want match give me each identifier has foo- , 7 characters 1 or more non-digit characters. if input this:
"foo-1234567\nfoo-1234a67\nfoo-123$5^7" i want array match this: ["foo-1234a67", "foo-123$5^7"].
regexes have tried:
/^foo-\d+$/gmi //nada /^foo-(\d){1,7}$/gmi //not close /^foo-\d*\d+\d*$/gmi //good matches > or < 7 characters /^foo-(?=^foo-\d*\d+\d*$)(?=pr-.{7})/gmi //empty string or null as always, code-golf, shortest code wins.
the regex match individual valid values simple: /^foo-\d{7}$/. i'd suggesting .split()ing on newlines , .filter()ing throw away values don't match regex:
let getinvalid = input => input.split("\n").filter(v => !/^foo-\d{7}$/.test(v)) // following has valid, outputs empty array: console.log( getinvalid("foo-1234567\nfoo-7654321\nfoo-1324536") ) // following has invalid, outputs them: console.log( getinvalid("foo-1234567\nfoo-1234a67\nxyz-1234567\nfoo-123$5^7") ) // works when there 1 value: console.log( getinvalid("foo-1234567") ) // following has invalid, outputs them: console.log( getinvalid("foo-123$5^7") ) edit: after spencer's prompting i've realised question asked match invalid values start "foo-" , right length. i.e., "foo-123$5^7" in desired output, "foo-123" , "xyz-1234567" not in output though they're invalid values. if that's case can use regex negative lookahead exclude values 7 digits:
/^foo-(?!\d{7}).{7}$/ in context:
let getinvalid = input => input.match(/^foo-(?!\d{7}).{7}$/gmi) console.log( getinvalid("foo-1234567\nfoo-1234a67\nfoo-123") ) console.log( getinvalid("foo-1234567\nfoo-11111117\nfoo-1239999") ) console.log( getinvalid("foo-12##567\nxyz-1234a67\nfoo-123$5^7") )
Comments
Post a Comment