Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get particular string part in javascript

I have a javascript string like "firstHalf_0_0_0" or secondHalf_0_0_0". Now I want to get the string before the string "Half" from above both strings using javascript.Please help me.

Thanks.

like image 713
manishjangir Avatar asked Mar 19 '12 07:03

manishjangir


People also ask

How do you find out the part of the string from a string?

To locate a substring in a string, use the indexOf() method.

How do you get part of a string after?

To get a part of a string, string. substring() method is used in javascript. Using this method we can get any part of a string that is before or after a particular character.

Can you slice a string in JavaScript?

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

How do substring () and substr () differ?

The difference between substring() and substr()The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .


2 Answers

var myString = "firstHalf_0_0_0";
var parts = myString.split("Half");
var thePart = parts[0];
like image 75
Config Avatar answered Oct 16 '22 15:10

Config


var str = 'firstHalf_0_0_0',
    part = str.match(/(\w+)Half/)[1];

alert(part); // Alerts "first"
like image 41
elclanrs Avatar answered Oct 16 '22 17:10

elclanrs