Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string only on first instance of specified character

In my code I split a string based on _ and grab the second item in the array.

var element = $(this).attr('class'); var field = element.split('_')[1]; 

Takes good_luck and provides me with luck. Works great!

But, now I have a class that looks like good_luck_buddy. How do I get my javascript to ignore the second _ and give me luck_buddy?

I found this var field = element.split(new char [] {'_'}, 2); in a c# stackoverflow answer but it doesn't work. I tried it over at jsFiddle...

like image 587
Ofeargall Avatar asked Jan 05 '11 18:01

Ofeargall


People also ask

How do you split a string only on the first instance of specified character?

To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.

How do I split a string with first space?

Using the split() Method For example, if we put the limit as n (n >0), it means that the pattern will be applied at most n-1 times. Here, we'll be using space (” “) as a regular expression to split the String on the first occurrence of space.

How do you split a string at a certain character?

To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do you get the first element to split?

To split a string and get the first element of the array, call the split() method on the string, passing it the separator as a parameter, and access the array element at index 0 . For example, str. split(',')[0] splits the string on each comma and returns the first array element. Copied!


2 Answers

Use capturing parentheses:

"good_luck_buddy".split(/_(.+)/)[1] "luck_buddy" 

They are defined as

If separator contains capturing parentheses, matched results are returned in the array.

So in this case we want to split at _.+ (i.e. split separator being a sub string starting with _) but also let the result contain some part of our separator (i.e. everything after _).

In this example our separator (matching _(.+)) is _luck_buddy and the captured group (within the separator) is lucky_buddy. Without the capturing parenthesis the luck_buddy (matching .+) would've not been included in the result array as it is the case with simple split that separators are not included in the result.

like image 123
Mark Avatar answered Sep 18 '22 21:09

Mark


What do you need regular expressions and arrays for?

myString = myString.substring(myString.indexOf('_')+1) 

var myString= "hello_there_how_are_you"  myString = myString.substring(myString.indexOf('_')+1)  console.log(myString)
like image 28
kennebec Avatar answered Sep 18 '22 21:09

kennebec