How do I make an expression to match absolutely anything (including whitespaces)?
Example:
Regex: I bought _____ sheep.
Matches: I bought sheep. I bought a sheep. I bought five sheep.
I tried using (.*)
, but that doesn't seem to be working.
(wildcard character) match anything, including line breaks. Throw in an * (asterisk), and it will match everything. Read more. \s (whitespace metacharacter) will match any whitespace character (space; tab; line break; ...), and \S (opposite of \s ) will match anything that is not a whitespace character.
Matching a Single Character Using Regex By default, the '. ' dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.
The method str. match(regexp) finds matches for regexp in the string str . If the regexp has flag g , then it returns an array of all matches as strings, without capturing groups and other details. If there are no matches, no matter if there's flag g or not, null is returned.
The dot matches all except newlines (\r\n). So use \s\S, which will match ALL characters.
Normally the dot matches any character except newlines.
So if .*
isn't working, set the "dot matches newlines, too" option (or use (?s).*
).
If you're using JavaScript, which doesn't have a "dotall" option, try [\s\S]*
. This means "match any number of characters that are either whitespace or non-whitespace" - effectively "match any string".
Another option that only works for JavaScript (and is not recognized by any other regex flavor) is [^]*
which also matches any string. But [\s\S]*
seems to be more widely used, perhaps because it's more portable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With