易讀,但是不全 https://larry850806.github.io/2016/06/23/regex/
var getUrlParam = function (name) {
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = decodeURIComponent(window.location.search).substr(1).match(reg);
if(r!=null)return unescape(r[2]); return '';
}
開頭或& 接上 name 接上 =0個以上非&的值 最後接 &或結束
JavaScript: RegExp constructor vs RegEx literal
The key difference is that literal REGEX can't accept dynamic input, i.e. from variables, whereas the constructor can, because the pattern is specified as a string.
Say you wanted to match one or more words from an array in a string:
var words = ['foo', 'bar', 'orange', 'platypus'];
var str = "Foo something nice orange what a lovely platypus";
str.match(new RegExp('\\b('+words.join('|')+')\\b'));
This would not be possible with a literal
/pattern/
, as anything between the two forward slashes is interpreted literally.
Note also the need to double-escape (i.e.
\\
) special characters when specifying patterns in this way, because we're doing so in a string - the first backslash must be escaped by the second so one of them makes it into the pattern. If there were only one, it would be interpreted by JS's string parser as an escaping character, and removed.RegEx 的方法
exec()
方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null
。
如果你只是为了判断是否匹配(true或 false),可以使用
RegExp.test()
方法,或者 String.search()
方法。RegEx literal的規則
字元
a: a 這個字元
0: 0 這個字元
.: 任意字元
RegExp | 說明 | 範例 |
/a/ | 含有字母 a 的字串 | "a","apple" |
/./ | 含有任意字元的字串 | "aaa","a","嗨" |
/a./ | 含有字母 a 後面接一個任意字元的字串 | "aaa","apple" |
次數
*: 出現 0 次以上
?: 出現 0 次或 1 次
+: 出現一次以上
{2}: 出現兩次
{2,}: 出現兩次以上
{,10}: 出現十次以下
{2,5}: 出現兩次到五次
RegExp | 說明 | 範例 |
/a*/ | 包含 0 次以上的 a | "apple","hello" |
/ab*/ | 包含一個 a,後面至少 0 個 b | "a","ab" |
/a?/ | 包含空字串或一個 a | "c","","app" |
/123a+/ | 包含 123 後面有一個以上的 a | "123a","123app" |
/123a{1,2}/ | 包含 123 後面出現一個或兩個 a | "1123a","123aaa" |
頭尾
^: 開頭
$: 結尾
RegExp | 說明 | 範例 |
/^app/ | 開頭是 app 的字串 | "app","apple" |
/ry$/ | 結尾是 ry 的字串 | "Larry" |
/^abcd$/ | 開頭結尾中間只有 abcd 的字串 | "abcd" |
/^La.*le$/ | 開頭是 La 尾巴是 le 的字串 | "Larry loves apple" |
比對多個字元
[]: 括號內的任何字元
[^]: 不在括號內的任何字元
RegExp | 說明 | 範例 |
/^[aeiou]/ | 開頭是小寫母音的字串 | "apple","oh" |
/[^aeiouAEIOU]$/ | 結尾不是母音的字串 | "Larry","ok" |
/^[aeiou]{3}$/ | 三個小寫母音組成的字串 | "aaa","aeu" |
/^[^aeiou]*$/ | 不包含小寫母音的字串 | "hEllO","ApplE" |
/[0-9]/ | 含數字的字串 | "app1e","123" |
/[a-z]/ | 含小寫字母的字串 | "12a45","aaa" |
/^[^a-zA-Z]$/ | 不含英文字母的字串 | "123","345" |
括號的單位一個字元
\n |
Where n is a positive integer, a back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses).
For example,
/apple(,)\sorange\1/ matches 'apple, orange,' in "apple, orange, cherry, peach." 即 'apple, orange, cherry, peach'.match(/apple(,)\sorange\1/)
For ezample, 'foobarbarfoo'.match(/(foo)(bar)\2\1/)
|
特殊字元
\.: "." 這個字元,直接寫 /./ 會被判斷成任意字元
\+: "+" 這個字元,類似的還有 \?, \*
\(: "(" 這個字元,類似的還有 \), \[, \]
\\: "\" 這個字元
\d: 任何數字字元,等同 [0-9]
\D: 任何非數字字元,等同 [^0-9]
\w: 任何數字字母底線,等同 [A-Za-z0-9_]
\W: 任何非數字字母底線,等同 [^A-Za-z0-9_]
\s: 任何空白字元(空白,換行,tab),等同 [ \f\n\r\t\v]
\S: 任何非空白字元(空白,換行,tab),等同 [^ \f\n\r\t\v]
實例
RegExp | 說明 | 範例 |
/^\d{4}-\d{2}-\d{2}$/ | 西元生日格式 | "1996-08-06" |
/^[A-Z]\d{9}$/ | 身分證字號 | "A123456789" |
/^09\d{8}$/ | 手機號碼 | "0912345678" |
/^[^aeiou]*$/ | 不包含小寫母音的字串 | "hEllO","ApplE" |
/^.*@gmail\.com$/ | gmail 信箱 | "test@gmail.com" |
/^[0-9\+\-\*\/]*$/ | 四則運算算式 | "1+2*3" |
在各語言中使用(使用時不要有空白)
C++
註: C++ 11 才開始支援 regex
#include<regex>
#include<string>
using namespace std;
int main(){
regex pattern("^[0-9]{4}-[0-9]{2}-[0-9]{2}$");
string str = "1996-08-06";
if(regex_match(str, pattern)){
// doSomething
}
}
Java
public class Example {
public static void main(String[] args) {
String pattern = "^[0-9]{4}-[0-9]{2}-[0-9]{2}$";
String str = "1996-08-06";
if(str.matches(pattern)){
//doSomething
}
}
}
JavaScript
var pattern = new RegExp('[0-9]{4}-[0-9]{2}-[0-9]{2}');
var str = '1996-08-06';
if(str.match(pattern)){
// doSomething
}
new Regexp()
form must compile the regular expression each time it is called - so performance will be better with the literal version: Use it whenever possible! – Doin Dec 18 '16 at 14:41eval()
.) – Utkanos Dec 18 '16 at 18:00