r'(^|^A)(\S+)(B$|$)'
results to matches everything, which actually equals to ^\S$.
How to write one matches "begins with A or ends with B, may both but not neither?"
PS: I also need refer to group (\S+) in the substring module.
Example:
Match Aanything
, anythingB
, and refer anything
group in the replace.
(^A.*B$)|(^A.*$)|(^.*B$)
Is this the desired behavior?
var rx = /^((?:A)?)(.*?)((?:B)?)$/;
"Aanything".match(rx)
> ["Aanything", "A", "anything", ""]
"anythingB".match(rx)
> ["anythingB", "", "anything", "B"]
"AanythingB".match(rx)
> ["AanythingB", "A", "anything", "B"]
"anything".match(rx)
> ["anything", "", "anything", ""]
"AanythingB".replace(rx, '$1nothing$3');
> "AnothingB"
"AanythingB".replace(rx, '$2');
> "anything"
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