I have a label containing text. I need to get two text elements separated by a "-" character. How to do this using regular expressions in jQuery?
I would suggest using split rather than regex. You can get both elements using string.split(" - "); . This will return an array of strings with elements broken into "-".
string.split(" - ");
Why do you need regex?
var title = "Hello - World!"; var parts = title.split(' - '); alert(parts[0] + '\n' + parts[1]);
If I am missing something, regex is not necessary and just causes unnecessary overhead.
Why use a regex?
fiddle reference
var array = $('label').map(function(){ return this.innerHTML.split('-'); }).get();
Markup
<label>test-test2</label> <label>test1-test3</label>
This will result in an array of individual text elements. See Console Output in a script link.
var mySplitResult = $('#myLabel').val().split("-");
mySplitResult[0] will contain the first bit and also mySplitResult[1] will contain the second bit.
mySplitResult[0]
mySplitResult[1]
Source: https://habr.com/ru/post/1346264/More articles:Problem with NOLOCK on SQL 2008 using a temporary table and selecting statements - sql-server-2008Test vectors for version 5 UUID generation algorithm (hash conversion to guid)? - uuidWord 2010: How to edit / view existing add-on menu commands and custom toolbar items - ms-wordPhp cannot use stdClass object as array - jsonGWT Designer with smartGWT: no palettes for smart widgets (XML UIBinder mode) - javaCoreVideo iOS memory leaks - iosdownload google maps apis maps if necessary? jquery.getScript ()? - javascriptSlow deployment for a large Rails 3 application using Ruby 1.9.2 - ruby | fooobar.comHow to create SQLite timestamp value in PHP - phpCan I use a reliable connection (SSPI) with the SQLDMO API? - c #All Articles