Regular expressions can be used to perform all types of text search and text replace operations.
Syntax
==/pattern/modifiers;==
modifiers
| modifier | description | | ——– | ——————————————– | | i | case-intensive match | | g | global match(find all after the first match) | | m | multiline match |
patterns
| pattern | description | | ——- | ———————————– | | [abc] | find any char in the brackets | | [0-9] | find any digits between the brakets | | (x|y) | fid any alternatives seperated with | |
metacharacters
| metacharacter | description | | ————- | ————————————— | | \d | find a digit | | \s | find a whitespace charactor | | \b | find a match at the beginning of a word | | \uxxxx | find the unicode charactor |
qualitifiers
| qualitifier | description | | ———– | ———— | | n+ | Matches any string that contains at least one n | | n* | Matches any string that contains zero or more occurrences of n | | n? |Matches any string that contains zero or one occurrences of n |
<html>
<body>
<p id="atLeastOne"></p> //100, 1000
<p id="zeroOrMore"></p> //1,100,1000
<p id="zeroOrOne"></p> //1,10,10
<script>
let text = "1, 100 or 1000";
let resultOne = text.match(/10+/g);
let resultTwo = text.match(/10*/g);
let resultThree = text.match(/10?/g);
document.getElementById("atLeastOne").innerHTML = resultOne;
document.getElementById("zeroOrMore").innerHTML = resultTwo;
document.getElementById("zeroOrOne").innerHTML = resultThree;
</script>
</body>
</html>