Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the text after a specific word

Tags:

Let say I have the following strings:

var Y = "TEST"  var X = "abc 123 TEST 456 def" 

I would like to get the string from X that comes after the word that specified in Y.

In this example it will be:

var Z = " 456 def" 
like image 915
Wolf Avatar asked Sep 03 '14 08:09

Wolf


People also ask

How do I extract text before and after a specific word in Excel?

Extract text before or after space with formula in Excel Select a blank cell, and type this formula =LEFT(A1,(FIND(" ",A1,1)-1)) (A1 is the first cell of the list you want to extract text) , and press Enter button.

How do I extract text after a specific character?

To get text following a specific character, you use a slightly different approach: get the position of the character with either SEARCH or FIND, subtract that number from the total string length returned by the LEN function, and extract that many characters from the end of the string.


1 Answers

Probably the fastest way will be to use .slice, .substr or .substring:

var Z = X.slice(X.indexOf(Y) + Y.length); 

However there are some other alternatives, like with regular expressions:

var Z = X.replace(new RegExp('.*' + Y), ''); 

Or the one with arrays, proposed by @AustinBrunkhorst in the comments:

var Z = X.split(Y).pop(); 
like image 161
VisioN Avatar answered Oct 01 '22 16:10

VisioN