I'm trying to parse the end of a url and redirect depending on it's ending.
For example, if I'm going to www.something.com/foo/1/
I want to redirect to www.something.com/foo/1/bar
. The 1 in this case can be any positive integer.
What I've been trying with but can't get to work is:
if (window.location.href.endsWith('/foo/\\d/')) { fetch stuff and redirect } else { do something else }
Which doesn't work, I'm guessing endsWith
can't parse regex.
Thanks in advance,
Peter
In your code endsWith('/foo/\\d/')
check the string is ending with that particular string argument and won't work as a regex.
For checking using regex use RegExp#test
method with regex /foo\\/\\d{1,3}\\/$/
.
if(/foo\/\d{1,3}\/$/.test(window.location.href))
You can regex match for the end of string instead,
/.*foo\d/$/
My regex might be wrong, but you can use the $ to denote end of string and just do a regular regex.test
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.