簡體   English   中英

正則表達式:在另一個字符串中間匹配url字符串

[英]Regex: Match url string in the middle of another string

正在學習正則表達式,但目前我對這種模式有些困惑。

我需要在url字符串中匹配我的字符串,例如:

如果url包含字符串'/example/regex' ,則應返回true。

/REGEXFOR:'/example/regex'/.test('http://test.com/example/regex/new') // => true
/REGEXFOR:'/example/regex'/.test('http://test.com/example/regex/new/boo') // => false

謝謝!

在您使用$的情況下,您可以指示正則表達式的結尾

/\/example\/regex\/\w*$/.test('http:\/\/test.com\/example\/regex\/new') => true
/\/example\/regex\/\w*$/.test('http:\/\/test.com\/example\/regex\/new\/boo') => false

如果您不想再使用任何斜杠,請在字母和數字字符上使用\\ w *

您只需逃脫反斜杠,就需要對其進行清理。

 /\/example\/regex/.test('http://test.com/example/regex/new/boo')

根據您提供的示例,盡管兩者都應驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM