markdown 正则表达式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 正则表达式相关的知识,希望对你有一定的参考价值。
. - Any Character Except New Line
\d - Digit (0-9)
\D - Not a Digit (0-9)
\w - Word Character (a-z, A-Z, 0-9,_) ( it is called shorthand character classes)
\W - Not a word character
\s - Whitespace (space, tab, newline, form feed) [ \r\t\f\n\v]
\S - Not Whitespace (space, tab, newline)
\b - Word Boundary : limite de mot
\B - Not a Word Boundary
^ - Beginning of a String (or line, row ) [^ \r\t\f\n\v]
$ - End of a String
[] - Matches Characters in Brackets ( it is called character set )
[^ ] - Matches Characters NOT in Brackets
| - Either value OR value e.g. /[yes|not]/
() - Group
Quantifiers:
* - 0 or More of the preceding token( greedy) equivalent to {0,}
+ - 1 or More equivalent to {1,}
? - 0 or One (lazy)(e.g. select 'Mr.' or 'Mr' regex: /Mr\.?/ ) equivalent to {0,1}
{3} - Exact Number
{3,4} - Range of Numbers (Minimum, Maximum)
{3,} - 3 or more
Positive and Negative lookahead:
let's say I want to select only the box where it followd by box-shadow
/box(?=-shadow)/ will select box from box-shadow
box-sizing
box-shadow
let's say I want all box selected when it is not followed by -shadow
/box(?!-shadow)/
A more practical use of lookaheads is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:
*/
let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password); // Returns true
/*********** GREEDY AND LAZY********************************************************************/
var s="aaaabbbb";
console.log( s.match(/a+b/)+"" ); //output: aaaab
console.log( s.match(/a+b+/)+"" ); //output: aaaabbbb
//See the example above, we can conclude that the "personality" of regular expression is "greedy".
//It always tries to match the longest results
var s="100001,111,12222221,222,3333,12321,14441";
var match1=s.match(/1.*1/g)
var match2=s.match(/1.+1/g)
console.log(match1) //in fact result is [ '100001,111,12222221,222,3333,12321,14441' ]
console.log(match2) //in fact result is [ '100001,111,12222221,222,3333,12321,14441' ]
//Oh no! This is not what I want. The "greedy" of regular expression is the "original sin". It matches the longest string: the string itself.
// How to solve this problem? Very simple, another usage of the "?" can make the regular expression become "lazy".
var match=s.match(/1.+?1/g)
//the result should be [ '100001', '111', '12222221', '12321', '14441' ]
console.log(match) //output: [ '100001', '111', '12222221', '12321', '14441' ]
/*********** END GREEDY AND LAZY********************************************************************/
/*********** GROUP USING $1,$2 AND BACKREFERENCE \1,\2,ETC... **************************************/
// backreference with replace() method
let str ="221-443-5764"
var regEx = /(\(?\d{3}\)?)-(\d{3})-(\d{4})/g
// group 0 will be : "221-443-5764"
// group 1 will be : "443"
// group 2 will be : "5764"
// $1 is used to reference the first group by using replace() method of String.
// \1 is used when evaluating the expression with test, match...
str = "These are some phone number 345-xxx-xxxx and also some other phone number 234-xxx-xxxx and again another phone number (342)-xxx-xxxx.";
str.replace(regEx,'$1-xxx-xxx');
// backreference in regular expression
var str="abba baab green glass roof";
var reg=/(.)(.)\2\1/g
console.log( str.match(reg)) //output: [ 'abba', 'baab' ]
/*********** GROUP USING $1,$2 AND BACKREFERENCE \1,\2,ETC... **************************************/
/*
#### Sample Regexs ####
[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-]
*/
/*Match a Literal String with Different Possibilities*/
// let petString = "James has a pet cat.";
// let petRegex = /dog|cat|bird|fish/; // Change this line
// let result = petRegex.test(petString);
/* Match method */
//console.log("Hello, World!, Hello, World!".match(/Hello/));
// Returns ["Hello", index: 0, input: "Hello, World!", groups: undefined]
/*
0:"Hello"
groups:undefined
index:0
input:"Hello, World!"
length:1
__proto__:Array(0)
*/
// let ourStr = "Regular expressions";
// let ourRegex = /expressions/;
// console.log(ourStr.match(ourRegex));
// let test = ourStr.match(ourRegex);
// console.log(`extraction ${test}`);
// Returns ["expressions"]
// let quoteSample = "The five boxing wizards jump quickly.";
// let alphabetRegexV2 = /\w/g; // Change this line
// let result = quoteSample.match(alphabetRegexV2);
// console.log(result);
// let sampleWord = "astronaut0";
// let pwRegex = /(?=\D*\d\d)/; // Change this line
// let result = pwRegex.test(sampleWord);
// console.log(result);
//
let hello = " Hello, World! ";
let wsRegex = /\s{2,}/g;
let test = " Hello, World! ".match(wsRegex);
console.log(test);
// select only what you need to replace
let result = hello.replace(wsRegex,'' );
console.log(result);
以上是关于markdown 正则表达式的主要内容,如果未能解决你的问题,请参考以下文章