JavaScript Regex - How to Use Regular Expressions in JavaScript



JavaScript Regular Expressions

  • A regular expression is a order of characters that forms to get the search pattern.
  • The search pattern can be used for search and replace the text.
js-regex

Sample Code

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript String Methods</h1>
<p id="para1"></p>
<p id="para2">welcome to wikitechy</p>
<script>
let text = "Visit kaashiv"; 
document.getElementById("para1").innerHTML = text.search("kaashiv");

let text1 = document.getElementById("para2").innerHTML;
document.getElementById("para2").innerHTML = text1.replace(/wikitechy/i,"kaashiv");
</script>
</body>
</html>

Output

JavaScript String Methods
6

welcome to kaashiv
Modifier Description
i Used for case-insensitive matching
g Used for global match find all matches rather than stopping after the first match
m Used for multi-line matching
Modifier Description
[abc] Find any of the characters between the brackets
[0-9] Find any of the digits between the brackets
(x|y) Find any of the alternatives separated with |
Modifier Description
\d Used for finding the digit
\s Used to find whitespace character
\b Finding a match at the starting of a word like this: \bJAVASCRIPT, or at the ending of a word like this: JAVASCRIPT\b
\uxxxx Used for findinf the Unicode character that specified by the hexadecimal number xxxx

Related Searches to JavaScript Regex - How to Use Regular Expressions in JavaScript