0

I have a string like

sadd*sadasdsdsad*sdadsdasdad*asdsadsadsadasdsdsd*asdasdsadasdasdasdasdasdasd*asdasdadasdsdasd*asdsaddasdasdas*

now i want all consecutive *'s to be replaced with <b> & </b> respectively. Above string will become

sadd<b>sadasdsdsad</b>sdadsdasdad<b>asdsadsadsadasdsdsd</b>asdasdsadasdasdasdasdasdasd<b>asdasdadasdsdasd</b>asdsaddasdasdas*sdfdsfsd

the last * is not replaced because there is no consecutive pair of star left.

1 Answer 1

2

Seems like you could simply do

\*(.+?)((?=$)|\*)
# match a star
# anything else lazily afterwards
# the end of the string
# or another star

And replace this with <b>$1</b>, see a demo on regex101.com.

Sign up to request clarification or add additional context in comments.

3 Comments

the last star is getting replaced to <b></b> check same demo at regex101.com
@user609306: Right, adjusted to match at least one character. It's always better to escape special characters like the star.
finally i used this one in PHP echo preg_replace('/*(.*?)*/', '<b>$1</b>',$string);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.