I'm making a text editor/note-taking application and I'm adding some formatting options. One of them is the ability to "quote"
My regular expression looks like this: ^>.*
This matches: >hello world!
, but not: >hello world!
(at beginning is space)
Also I do not want it to match something like <div></div>
.
CodePudding user response:
Special RegEx character ^
means that the patter must match by the beginning of the string.
It matches >hello world!
because character >
is the first in the string.
But in the second case the first character is space (or tab).
If you want to match with whitespace you need to add \s
and to make it optional use ?
,
so the expression would be: ^\s?>.*
If you want to mach against any number of whitespace then ^\s*>.*
Also I do not want it to match something like
<div></div>
.
Those expressions won't match <div></div>
, because it doesn't start with >
.
But just in case, if we are talking about HTML and RegEx then read Using regular expressions to parse HTML: why not?