简体   繁体   中英

RegEx wrap ***** (stars) in tags within string in JS

I have a string foo *** ; I would like that to be foo <span>***</span>

However, the number of * could be any amount, so I think I need a JS replace ???

Something like this will do the trick:

"foo ***".replace(/([\*]+)/g,"<span>$1</span>");

or, more generally,

str.replace(/([\*]+)/g,"<span>$1</span>");

You can see it in action here

Try this code:

var str = 'foo ***';
str = str.replace(/(\*+)/g,function(arg){
    return '<span>' + arg + '</span>';
});

Or also:

var str = 'foo ***';
str = str.replace(/\*+/g,'<span>$1</span>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM