javascript - Extract a certain part of a string after a key phrase - Stack Overflow

I was wonder the best way one could look for a specific phrase in a string and then take everything aft

I was wonder the best way one could look for a specific phrase in a string and then take everything after it and store that in a new variable. For example if...

var str = 'hello how are you doing the what is a dinosaur search Jim carrey';

Everything after 'search ' or 'search for ', in this case 'Jim carrey' should be stored in a new var— let's call it val.

Also, if there are two or more instances of the phrase 'search ' or 'search for ' then only store what is after the latest one in val.

For example: if str = 'wele I am search pie recipie search quick chili recipie' then val would be 'quick chili recipie' Or If str = 'Today is 78 degrees search good weather and sunny in other words a perfect day for search for when does summer end' then val would be 'when does summer end'.

I was wonder the best way one could look for a specific phrase in a string and then take everything after it and store that in a new variable. For example if...

var str = 'hello how are you doing the what is a dinosaur search Jim carrey';

Everything after 'search ' or 'search for ', in this case 'Jim carrey' should be stored in a new var— let's call it val.

Also, if there are two or more instances of the phrase 'search ' or 'search for ' then only store what is after the latest one in val.

For example: if str = 'wele I am search pie recipie search quick chili recipie' then val would be 'quick chili recipie' Or If str = 'Today is 78 degrees search good weather and sunny in other words a perfect day for search for when does summer end' then val would be 'when does summer end'.

Share Improve this question asked Mar 21, 2013 at 4:30 IMUXIxDIMUXIxD 1,2275 gold badges23 silver badges44 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 6

Try this RegExp :

var str = 'Today is 78 degrees search good weather and sunny in other words a perfect day for search for when does summer end';
var arr = str.split(/ search (for){0,1}/);

alert(arr[arr.length-1]);

Legendinmaking's answer returns the phrase with a space at beginning and to return everything after search or search for use this

var str = 'wele I am search pie recipie search quick chili recipie';
 var param='search';
 var param1='search for';
 var n=str.lastIndexOf(param);
 var n1=str.lastIndexOf(param1);
 n=Math.max(n,n1);
 alert(str.substring(n+param.length + 1,str.length));

try

var str = "wele I am search pie recipie search quick chili recipie";
var m = str.split('search');
alert(m.slice(-1));

or

  str.split('search').pop() // will do same thing

working DEMO

i am not sure it is the best way of doing it but it works and you can try to change srt value at jsbin: http://jsbin./uguzup/1/edit

var str = 'Today is 78 degrees search good weather and sunny in other words a perfect day for search for when does summer end';
var val = str.split('search for').slice(-1)[0].split('search').slice(-1)[0]

Try this

var str = 'wele I am search pie recipie search quick chili recipie';
    var param='search';
    var n=str.lastIndexOf(param);
    alert(str.substring(n+param.length,str.length));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742256828a4410174.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信