最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - URL "starts with" using regex? - Stack Overflow

programmeradmin2浏览0评论

Say I have the following url:

How can I create a regex that will match any url starting with that url segment above?

For example:

(should match)

(should match)

(should match)

(should not match)

(should not match)

(should not match)

Yes, obviously I could just call string.indexOf(url) == 0 to do a "starts with" check, but I specifically need a regular expression because I have to provide one to a third-party library.

Say I have the following url:

http://example.com/api

How can I create a regex that will match any url starting with that url segment above?

For example:

http://example.com/api/auth (should match)

http://example.com/api/orders (should match)

http://example.com/api/products (should match)

http://example.com/auth (should not match)

http://examples.com/api/auth (should not match)

https://example.com/api/auth (should not match)

Yes, obviously I could just call string.indexOf(url) == 0 to do a "starts with" check, but I specifically need a regular expression because I have to provide one to a third-party library.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Sep 19, 2014 at 9:14 XåpplI'-I0llwlg'I -XåpplI'-I0llwlg'I - 22.4k28 gold badges107 silver badges154 bronze badges 1
  • Try a thing or 2 yourself, first. Then tell us if you have to provide a string, from which a regex will be created (new RegExp argument != /pattern/) – Elias Van Ootegem Commented Sep 19, 2014 at 9:19
Add a comment  | 

5 Answers 5

Reset to default 13

The ^ modifier at the start of the expression means "string must start with":

/^http:\/\/example\.com\/api/

If you were using a different language which supports alternative delimiters, then it would probably be worth doing since the URL will contain /s in it. This doesn't work in Javascript (h/t T.J. Crowder) but does in languages like PHP (just mentioning it for completeness):

#^http://example\.com/api#

You could use this in JavaScript, though:

new RegExp("^http://example\\.com/api")

It's also worth noting that this will match http://example.com/apis-are-for-losers/something, because you're not testing for a / after api - just something to bear in mind. To solve that, you can use an alternation at the end requiring either that you be at the end of the string or that the next character be a /:

/^http:\/\/example\.com\/api(?:$|\/)/

new RegExp("^http://example\\.com/api(?:$|/)")

Why a regex if your search term is constant?

if (str.substr(0, 22) == 'http://example.com/api') console.log('OK');
^http:\/\/example\.com\/api.*

Regex link

Since it's javascript you can try this

 var str = "You should match this string that starts with"; 
 var res = str.match(/^You should match.*/);
 alert(res);

You can use an 'anchor' to match the start (or end) of a string.

More info: http://www.regular-expressions.info/anchors.html

发布评论

评论列表(0)

  1. 暂无评论