How to use regexp in javascript
By FoxLearn 8/29/2024 2:01:54 AM 108
Here’s a basic guide to using regexp in javascript.
You can use the RegExp
constructor, which is useful when you need to build regex patterns dynamically.
For example, to match the word "hello":
let regex = new RegExp('hello'); let regex = new RegExp('\\d+'); // Matches one or more digits
Use the .test()
method to check if a pattern is found in a string:
let regex = /hello/; console.log(regex.test('hello world')); // true console.log(regex.test('goodbye world')); // false
Use the .exec()
method to find matches and get detailed information:
let regex = /hello/; let result = regex.exec('hello world'); console.log(result); // ['hello', index: 0, input: 'hello world', groups: undefined]
The match() method of String values retrieves the result of matching this string against a regular expression.
let regex = /hello/g; let matches = 'hello world, hello universe'.match(regex); console.log(matches); // ['hello', 'hello']
Use the g
(global) flag to match all occurrences in a string.
Regex Flags
g
: Global search (find all matches).i
: Case-insensitive search.m
: Multi-line search.s
: Dot matches newlines (allows.
to match newline characters).u
: Unicode (treats the pattern as a sequence of Unicode code points).y
: Sticky (matches from the current position in the string).
Example Regex Patterns
/\d+/
(matches one or more digits)/\D+/
(matches one or more non-digits)/\s+/
(matches one or more whitespace characters)/\S+/
(matches one or more non-whitespace characters)/\w+/
(matches one or more word characters)/\W+/
(matches one or more non-word characters)[abc]
(Find any character between the brackets)[^abc]
(Find any character NOT between the brackets)[0-9]
(Find any character between the brackets (any digit))[^0-9]
(Find any character NOT between the brackets (any non-digit)(x|y)
(Find any of the alternatives specified).
(Find a single character, except newline or line terminator)\w
(Find a word character)\W
(Find a non-word character)\d
(Find a digit)\D
(Find a non-digit character)\s
(Find a whitespace character)\S
(Find a non-whitespace character)\b
(Find a match at the beginning/end of a word, beginning like this: \bHI, end like this: HI\b)\B
(Find a match, but not at the beginning/end of a word)\0
(Find a NULL character)\n
(Find a new line character)\f
(Find a form feed character)\r
(Find a carriage return character)\t
(Find a tab character)\v
(Find a vertical tab character)\xxx
(Find the character specified by an octal number xxx)\xdd
(Find the character specified by a hexadecimal number dd)\udddd
(Find the Unicode character specified by a hexadecimal number dddd)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)n{X}
(Matches any string that contains a sequence of X n's)n{X,Y}
(Matches any string that contains a sequence of X to Y n's)n{X,}
(Matches any string that contains a sequence of at least X n's)n$
(Matches any string with n at the end of it)^n
(Matches any string with n at the beginning of it)?=n
(Matches any string that is followed by a specific string n)?!n
(Matches any string that is not followed by a specific string n)
For example:
let pattern = /\d+/g; // Find all sequences of digits let str = "We have 2 apples and 12 oranges."; let result = str.match(pattern); // ['2', '12'] console.log(result); let replaced = str.replace(pattern, 'number'); console.log(replaced); // "I have number apples and number oranges."
Regular expressions can be quite complex, so it’s helpful to consult regex reference materials or tools for advanced patterns and debugging.
- How to get datatype in JavaScript
- How to convert string to boolean in Javascript
- How to fix 'Fetch request to https:www.google.com fails with CORS'
- How to Check Internet Connection Status in Javascript
- How to download file using JavaScript
- How to get selected option in JQuery
- How to get data attribute JavaScript
- How to Convert a String to an Integer in JavaScript