php - Regex starts with x or x prefixed or suffixed -
i'm trying pattern match string following convert every line list item <li>:
-give on result &second new text -the third text paragraph without list. -new list here in natural language: match every string starts - , ended new line sign \n
i tried following pattern works fine:
/^([-|-]\w+\s*.*)?\n*$/gum of course can write without square brackets ^(-\w+\s*.*)?\n*$ debugging used described.
in example above, when replaces second - & ^([-|&]\w+\s*.*)?\n*$ works fine , mtaches the second line of smaple string. however, not able make matches - prefixed white space or suffixed white space.
i changed sample string to:
- give on result &second new text -the third text paragraph without list. -new list here and tried following pattern:
/^([-|\- |&| -]\w+\s*.*)?\n*$/gum however, failed match suffixed or prefixed - white space.
here are live demo original working pattern:
^(\h*(?:-|&)\h*\w+\s*.*)\n*$ you can try this.| inside [] has no special meaning.see demo.
https://regex101.com/r/ns2lt4/3
a string may start whitespace, should have either - or & may have spaces ahead. should have @ least 1 alphanumeric characters may have space ahead. can have or nothing. in end, eat newlines consume or none if can't.
Comments
Post a Comment