'This is *italics*'.replace(/\*(.+)\*/g, '<i>$1</i>');
- The starting and ending
/are defining that the thing in between is actually a
- We need to check for asterisks at the start and at the end, but * is a quantity selector in the RegExp, therefore we need to escape them using a
\(basically saying "hey, the next chracter is not an actual selector, but something literal")
- Next we need to specify that we need to check for any character between those asterisks (that's the
.), appearing more than once (that's the
- Finally we need to
groupthis and tell the RegExp that what we want to remember is the thing between the asterisks and not the whole thing, that's where the
parenthesiscome to action.
- Using those parenthesis, we can do
nis the matched quantity number, in this case
1) in the replacing string to replace for the matching group
GMaiolo posted this
Have an answer?