javascript - Getting words separated by " / " with Regex -
i have following fragment of code:
html
<div class="colors"> <h1>colors:</h1> <div class="recipe"></div> </div> <div class="numbers"> <h1>numbers:</h1> <div class="recipe"></div> </div> <div class="people"> <h1>people:</h1> <div class="recipe"></div> </div>
javascript
var colors = 'yellow / black / purple', numbers = '5 / 15 / 25', people = 'brad pitt / leonardo dicaprio / anne hathaway'; $('.colors .recipe').html(colors.replace(/(\w+)\/*/g, '<em>$1</em><br>')); $('.numbers .recipe').html(numbers.replace(/(\w+)\/*/g, '<em>$1</em><br>')); $('.people .recipe').html(people.replace(/(\w+)\/*/g, '<em>$1</em><br>'));
i not regular expression, i'm getting unexpected results on rendering separated values on respective recipes (you can see more on jsfiddle posted above).
i mean, following results showing me:
[...]
brad
pitt
/ leonardo
dicaprio
[...]
and want , need this:
brad pitt
leonardo dicaprio
no slashes, no separated names/surnames.
you dont need regex this. split method can job:
var colors = 'yellow / black / purple', numbers = '5 / 15 / 25', people = 'brad pitt / leonardo dicaprio / anne hathaway'; function wrapem(e) { return "<em>" + e + "</em>"; } people.split(" / ").join("<br/>"); $('.colors .recipe').html(colors.split(" / ").map(wrapem).join("<br/>")); $('.numbers .recipe').html(numbers.split(" / ").map(wrapem).join("<br/>")); $('.people .recipe').html(people.split(" / ").map(wrapem).join("<br/>"));
split name suggests, splits string array using /
delimiter.
Comments
Post a Comment