簡體   English   中英

從兩個特定字符之間的另一個字符串中提取一個字符串

[英]Extract a string from another string between two specific characters

我有如下結果字符串

*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 ::: |

我想在第一個實例“ *”(星號)和“:”(分號)之間加上單詞,例如這里結果應為“ WTY ”。 我已嘗試在下面進行操作,但未按預期工作。

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

任何人都可以指出錯誤或其他更好的解決方案嗎?

使用正則表達式

 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