[英]Regex for match first and last words in a sentence javascript?
我在模式匹配方面遇到了一些問題。 我的句子中有"Pyramids are mindblowingly awesome"
我想寫一個正則表達式,它與金字塔匹配並且很棒,也就是說,句子的首尾都應該與pyramids
匹配並且awesome
。 我正在考慮不區分大小寫。 我試過了/pyramids(...)/awesome/i
有人可以幫我/pyramids(...)/awesome/i
?
編輯:我正在尋找句子中的確切單詞。
謝謝。
您可以使用RegExp
/^\\w+|\\w+$/g
匹配字符串開頭的單詞或字符串結尾的單詞。
let str = "Pyramids are mindblowingly awesome"; let matches = str.match(/^\\w+|\\w+$/g); console.log(matches);
/^pyramids.*awesome$/i
將確保字符串以金字塔開頭和以超贊結尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.