简体   繁体   English

从两个特定字符之间的另一个字符串中提取一个字符串

[英]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 ". 我想在第一个实例“ *”(星号)和“:”(分号)之间加上单词,例如这里结果应为“ 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? 任何人都可以指出错误或其他更好的解决方案吗?

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); 

如果确定您的字符串包含这些符号,那么就功能而言,您可以编写如下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM