Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: how to get the text before and after a " - " with regex

I have a label, which contains text. I need to get the two text elements which are separated by " - ". How can I do this with regular expressions in jQuery?

like image 403
Markus Avatar asked Dec 13 '22 14:12

Markus


2 Answers

I would advise to use split rather than a regular expression. You can get both elements by using string.split(" - ");. This will return a string array with the elements split at the " - ".

like image 88
tvkanters Avatar answered Jan 12 '23 12:01

tvkanters


Why do you need regex?

var title = "Hello - World!";
var parts = title.split(' - ');
alert(parts[0] + '\n' + parts[1]);

Unless I'm missing something, regex is not necessary and just induces unnecessary overhead.

like image 40
Brad Christie Avatar answered Jan 12 '23 12:01

Brad Christie