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?
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 " - ".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With