JavaScript RegExp s Modifier
Last Updated :
07 Dec, 2024
The s modifier (also known as the "dotall" modifier) in JavaScript regular expressions allows the dot (.) metacharacter to match newline characters (\n) as well. Without the s modifier, the dot (.) matches any character except line breaks
JavaScript
let regex = /a.b/s;
let s = `a
b`;
console.log(regex.test(s));
- Regular Expression: We define a regular expression /a.b/s. The a is matched by the letter "a", the '.' is matched by any character (including newline), and b is matched by the letter "b".
- Test String: The string we are testing contains "a" followed by a newline character and then "b".
- Test: When we call regex.test(str), the regular expression successfully matches because the dot (.) can now match the newline between "a" and "b" due to the s modifier.
Syntax
let regex = /pattern/s;
- pattern: The regular expression pattern that can use the s modifier to enable dot (.) to match line breaks.
- s: The modifier that changes the behaviour of the dot (.) to match newline characters.
Real-World Use Cases of the s Modifier
1. Matching Multi-Line Strings
If you have a string that spans multiple lines, and you need to match a pattern that includes line breaks, the s modifier is ideal.
JavaScript
let regex = /start.*end/s;
let s = `start
middle
end`;
console.log(regex.test(s));
In this case, the pattern start.*end matches "start", followed by any characters (including line breaks), and finally "end".
2. Handling Text Blocks with Line Breaks
When parsing text with multiple lines (such as a block of HTML, a code block, or a formatted document), the 's' modifier lets you treat the text as a single continuous string.
JavaScript
let regex = /<div>.*<\/div>/s;
let html = `<div>
<p>Hello</p>
</div>`;
console.log(regex.test(html));
The regular expression matches the <div> tag and everything inside it, including line breaks.
3. Extracting Data from Multi-Line Logs
When dealing with logs or multi-line content, you might need to extract patterns that span multiple lines. The s modifier helps in this case.
JavaScript
let regex = /ERROR.*stack trace/s;
let data = `INFO: Everything is fine.
ERROR: Something went wrong
stack trace: at ...`;
console.log(regex.test(data));
Here, the ERROR.*stack trace pattern matches an error message that spans multiple lines.
4. Matching HTML Tags with Content Inside
The s modifier is also helpful for matching HTML tags that contain content with line breaks inside.
JavaScript
let regex = /<p>.*<\/p>/s;
let s = `<p>This is a paragraph.
It spans multiple lines.</p>`;
console.log(regex.test(s));
This matches a <p> tag with content that spans across multiple lines, thanks to the s modifier.
Key Points to Remember
- The s modifier allows the dot (.) in regular expressions to match newline characters (\n), which it does not do by default.
- It's particularly useful when you need to match patterns that span multiple lines or when your string includes line breaks.
- The s modifier is essential for parsing multi-line text, such as HTML or logs, where content might be spread across different lines.
- It makes working with text that includes line breaks much easier by eliminating the need to explicitly handle line breaks in the regular expression pattern.
Similar Reads
JavaScript RegExp g Modifier The g (global) modifier in JavaScript regular expressions is used to perform a global search. It ensures the pattern is matched multiple times throughout the entire string, rather than stopping at the first match.JavaScriptlet regex = /cat/g; let str = "cat, caterpillar, catch a cat"; let matches =
3 min read
JavaScript RegExp {X} Quantifier The RegExp m{X} Quantifier in JavaScript is used to find the match of any string that contains a sequence of m, X times where X is a number. JavaScriptlet str = "Geekkkksss@_123_$"; let regex = /k{2}/gi; let match = str.match(regex); console.log(match);Output[ 'kk', 'kk' ] Syntax: /m{X}/ // ornew Re
1 min read
JavaScript RegExp {X,} Quantifier The RegExp m{X, } Quantifier in JavaScript is used to find the match of any string that contains a sequence of m, at least X times, where X is a number.JavaScriptlet str = "GeeksforGeeeks e@_123_$"; let regex = /k{1,}/gi; let match = str.match(regex); console.log("Found " + match.length + " matches:
1 min read
JavaScript RegExp * Quantifier The RegExp m* Quantifier in JavaScript is used to find the match of any string that contains zero or more occurrences of match. JavaScriptlet str = "GeeksforGeeks@_123_G$"; let regex = /ke*/gi; let match = str.match(regex); console.log("Found " + match.length + " matches: " + match);OutputFound 2 ma
1 min read
JavaScript RegExp ?! Quantifier The RegExp ?!m Quantifier in JavaScript is used to find the match of any string which is not followed by a specific string m. JavaScript// 3-digits not followed by any numbers const str = "123Geeks12345@"; const regex = /\d{3}(?!\d)/g; const match = str.match(regex); console.log(match); Output[ '123
1 min read
JavaScript RegExp + Quantifier The m+ Quantifier in JavaScript is used to find the match of any string that contains at least one m. JavaScriptlet str = "GeeksforGeeks@_123_$"; let regex = /G+/gi; let match = str.match(regex); console.log("Found " + match.length + " matches: " + match);Syntax: /m+/ Example 1: Matches the presence
1 min read
JavaScript RegExp $ Quantifier The RegExp m$ Quantifier in JavaScript is used to find the match of any string which contains m at the end of it. JavaScriptlet str = "Geeksforh\nGeeks@_h"; let regex = /h$/gim; let match = str.match(regex); console.log("Found " + match.length + " matches: " + match);Syntax: /m$/ Example 1: Matches
1 min read
JavaScript RegExp {X,Y} Quantifier JavaScript RegExp {X,Y} Quantifier in JavaScript is used to find the match of any string that contains a sequence of m, X to Y times where X, Y must be numbered. JavaScript// Matches between 2 and 4 digits const regex = /\d{2,4}/; console.log(regex.test("123")); console.log(regex.test("12345")); con
1 min read
JavaScript RegExp Reference RegExp stands for Regular Expression. A regular expression is a sequence of characters that forms a search pattern. The search pattern can be used for text search and text to replace operations. A regular expression can be a single character or a more complicated pattern.Syntax:new RegExp("(Regular
4 min read
JavaScript RegExp S Metacharacter The RegExp \S Metacharacter in JavaScript is used to find the non-whitespace characters. The whitespace character can be a space/tab/new line/vertical character. It is the same as [^\t\n\r].JavaScriptlet str = "Geeky@128"; let regex = new RegExp("\\S", "g"); let match = str.match(regex); console.log
1 min read