How To Fix This Regex So It Replaces * Properly (between Words)?
I'm practicing regex. I thought of creating regex that turn * into , just like with Markdown: el = el.replace(/\*\b/g, '') el = el.replace(/\b\*|(\.|\,|\?|\!|\
Solution 1:
You can merge the two branches in your second regex since both end with \* pattern, like (\b|\.|,|\?|!|\*|---|\.{3}\s)\* (you may even merge the \.|,|\?|!|\* single char alternatives into [.,?!*]), and then use
var s = "Chicken teriy*ai*ki, r*ai*men noodles, spaghetti a la moneg*ai*sque.";
console.log(
s.replace(/\*\b([^]*?)(\b|[.,?!*]|---|\.{3}\s)\*/g, '<em>$1$2</em>')
)Details
\*\b- a*that is followed with a word char (letter, digit or_)([^]*?)- Group 1: any 0+ chars, as few as possible (may be replaced with a[\s\S]/[\d\D]/[\w\W]if you need more portability), up to the leftmost occurrence of(\b|[.,?!*]|---|\.{3}\s)- word boundary,.,,,?,!,*,---,...+ whitespace\*- a*char.
Solution 2:
This should work, it will wrap the characters between * signs into em tags, NOTE: this applies globally on the string provided.
const str = "something that has words surrounded with * signs"
str.replace(/\*(\w+)\*/g, "<em>$1</em>")
Solution 3:
Use regex \*([\w ^?.]*?)\*
Replace with <em>$1<\em>
el = el.replace(/\*([\w ^?.]*?)\*/g, '<em>$1<\em>')
Post a Comment for "How To Fix This Regex So It Replaces * Properly (between Words)?"