I know this is probably quite straightforward but cannot seem to find an example of what I'm trying to do.
Matching from start of a string, i want to match building numbers.
i.e.
60 would match 60A and 60 but not 6000
likewise
1 would match 1 and 1ABC but not 11
/^1[^\0-9]*
is like what i need, matching 1 and any non numeric value any number of times. (granted this is from expresso - (.net) but it doesn't work in there.
can anybody point me in the right direction?
thanks,
Sam
You can use regex /^1(?!\d)/
to match building 1.
THe (?!\d)
is a negative lookahead and says "match 1, as long as it isn't followed by another number".
e.g.
myString.match(/^1(?!\d)/)
If you want to put a variable in a regex, you can do something like:
var number = 60;
var re = new RegExp("^"+number+"(?!\\d)");
'60'.match(re); // => ["60"]
'60A'.match(re); // => ["60"]
'600 '.match(re); // => null
'a60A'.match(re); // => null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With