We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 7: Regular Expressions II
Day 7: Regular Expressions II
Sort by
recency
|
215 Discussions
|
Please Login in order to post a comment
The tests here dont cover a lot of overinclusion mistakes you could make with regex here. for instance, this regex will pass all tests:
/^[MDE][rs]{1,2}\.[a-zA-Z]*$/
but this would also match "Ess", "Err", "Msr", Mss", "Mrr", etc. as well as the legitimate prefixes. A better pattern could be something like:/^(Mr?s|[MDE]r)\.[a-zA-Z]*$/
you could also or together each possible prefix, which would be less performant but more readable, which could be preferrable if optimization isn't needed.const re = / ^ ( open_square_brackets ^ backslash . ^ backslash s closed_square_brackets * backslash . ) { 1 } open_square_brackets ^ backslash . closed_square_brackets * $ /
Start pattern has (anything except a dot or space, then a dot.). Start pattern repeats once. After that anything except a dot.
The backslashes do not show up correctly inside the square brackets so the pattern is written above with text.
this handels all the chases require which the top ones dont take into account like "Msr.abc"or "Ds.abc"
const re =/^(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.)[a-zA-Z]+$/