javascript URL“开始于”使用正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25930417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:14:35  来源:igfitidea点击:

URL "starts with" using regex?

javascriptregexstringcontainsstartswith

提问by X?pplI'-I0llwlg'I -

Say I have the following url:

假设我有以下网址:

http://example.com/api

http://example.com/api

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

如何创建一个正则表达式来匹配上面那个 url 段开头的任何 url ?

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)

http://example.com/api/auth(应该匹配)

http://example.com/api/orders(应该匹配)

http://example.com/api/products(应该匹配)

http://example.com/auth(不应该匹配)

http://examples.com/api/auth(不应该匹配)

https://example.com/api/auth(应该不匹配)

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

是的,显然我可以打电话string.indexOf(url) == 0做一个“开始”检查,但我特别需要一个正则表达式,因为我必须向第三方库提供一个。

回答by Joe

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 workin Javascript (h/t T.J. Crowder) but does in languages like PHP (just mentioning it for completeness):

如果您使用的是支持替代分隔符的不同语言,那么可能值得这样做,因为 URL 将包含/s。这在 Javascript (h/t TJ Crowder) 中不起作用,但在 PHP 等语言中起作用(只是为了完整性而提及):

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

You could use this in JavaScript, though:

不过,您可以在 JavaScript 中使用它:

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/apis-are-for-losers/something,因为您不是在测试/after api- 只是要记住一些事情。为了解决这个问题,您可以在末尾使用交替,要求您位于字符串的末尾或下一个字符是 a /

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

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

回答by Toto

Why a regex if your search term is constant?

如果您的搜索词是恒定的,为什么要使用正则表达式?

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

回答by andrex

Since it's javascript you can try this

因为它是 javascript 你可以试试这个

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

回答by Steven

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

Regex link

正则表达式链接

回答by Tom Ritsema

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

您可以使用“锚点”来匹配字符串的开头(或结尾)。

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

更多信息:http: //www.regular-expressions.info/anchors.html