Looking for a regex/replace function to take a user inputted string say, “John Smith’s Cool Page” and return a filename/url safe string like “john_smith_s_cool_page.html”, or something to that extent.
Looking for a regex/replace function to take a user inputted string say, John Smith’s
Share
Well, here’s one that replaces anything that’s not a letter or a number, and makes it all lower case, like your example.
Explanation:
The regular expression is
/[^a-z0-9]/gi. Well, actually thegiat the end is just a set of options that are used when the expression is used.imeans "ignore upper/lower case differences"gmeans "global", which really means that every match should be replaced, not just the first one.So what we’re looking as is really just
[^a-z0-9]. Let’s read it step-by-step:[and]define a "character class", which is a list of single-characters. If you’d write[one], then that would match either ‘o’ or ‘n’ or ‘e’.^at the start of the list of characters. That means it should match only characters not in the list.a-z0-9. Read this as "a through z and 0 through 9". It’s a short way of writingabcdefghijklmnopqrstuvwxyz0123456789.So basically, what the regular expression says is: "Find every letter that is not between ‘a’ and ‘z’ or between ‘0’ and ‘9’".