Bilal Hussain
Bilal Hussain

Reputation: 191

Extract a string from another string between two specific characters

I have a result string like below

*WTY:   this is Amy's home .
%mor:   pro:dem|this cop|be&3s n:prop|Amy's n|home .
%snd:   <00:00:00><00:01:23>
%WTY:   this is Amy’s home . errfr ::: |
*WTY:   last Sunday the television was showing the news report .
%mor:   adj|last n:prop|Sunday det|the n|television aux|be&PAST v|showing det|the n|news n|report .
%snd:   <00:01:77><00:06:65>
*WTY:   the dog come back again and play with Amy .
%mor:   det|the n|dog v|come adv|back adv|again conj|and v|play prep|with n:prop|Amy .
%snd:   <01:06:70><01:12:85>
%WTY:   {and} * (1.38) and_pfp (0.56) the dog (0.40) come back again err_m_s ::: and play with (0.56) Amy . err_m_s ::: |

I want to extact the words between first instance "*"(asterik) and ":"(semicolon) like here the result should b "WTY". I have tried doing below but it is not working as expected.

var ID=result.split('*').pop().split(':').shift();

Can anyone please point out the mistake or any other better solution?

Upvotes: 0

Views: 46

Answers (2)

Ilya Lyamkin
Ilya Lyamkin

Reputation: 384

If you are sure that your string contains these symbols, in terms of your functions you can write something like this:

result.split('*')[1].split(':')[0]

Upvotes: 0

baao
baao

Reputation: 73281

Use a regex

var string = `*WTY:   this is Amy's home .
%mor:   pro:dem|this cop|be&3s n:prop|Amy's n|home .
%snd:   <00:00:00><00:01:23>
%WTY:   this is Amy’s home . errfr ::: |
*WTY:   last Sunday the television was showing the news report .
%mor:   adj|last n:prop|Sunday det|the n|television aux|be&PAST v|showing det|the n|news n|report .
%snd:   <00:01:77><00:06:65>
*WTY:   the dog come back again and play with Amy .
%mor:   det|the n|dog v|come adv|back adv|again conj|and v|play prep|with n:prop|Amy .
%snd:   <01:06:70><01:12:85>
%WTY:   {and} * (1.38) and_pfp (0.56) the dog (0.40) come back again err_m_s ::: and play with (0.56) Amy . err_m_s ::: |`;

var match = string.match(/\*(\w+):/g);
console.log(match);

Upvotes: 1

Related Questions