Part of my regex is : /<a .*?(" "|"")(href)
. Within the (" "|"")
, I am trying to say match with either space or no space, but I can't get anything out of it. Ive also tried ("\s"|"")
with no results.
As a general rule, in English there is no space before and one space after a punctuation mark. Exceptions follow.
If the slash divides two words, there is no space. If it divides two phrases or sentences (or a single word from a phrase), it requires a space before and after. Please see CMOS 6.106.
Do you leave spaces around slashes in expressions such as “and/or” or do you open up spaces, using “and / or”? I've seen both styles. The online AP Stylebook (paid subscription required) says “No space on either side of the slash.”
The slash is an oblique slanting line punctuation mark /. Once used to mark periods and commas, the slash is now used to represent exclusive or inclusive or, division and fractions, and as a date separator.
"space or no space" is the same as "zero or one space", or perhaps "zero or more spaces", I'm not sure exactly what you want.
In the following discussion, I'm going to use <space>
to represent a single space, since a single space is hard to see in short code snippets. In the actual regular expression, you must use an actual space character.
zero-or-one-space is represented as a single space followed by a question mark (<space>?
). That will match exactly zero or one spaces. If you want to match zero or any number of spaces, replace the ?
with *
(eg: <space>*
)
If by "space" you actually mean "any whitespace character" (for example, a tab), you can use \s
which most regular expression engines translate as whitespace. So, zero-or-one of any whitespace character would be \s?
, and zero-or-more would be \s*
Other alternatives for the space
or no-space
problem (notice the spaces at the beginning in some):
[ ]{0,1} {0,1} [ ]? ?
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