2016-11-19 144 views
0

我有一個結果字符串像下面從另一字符串中提取字符串兩個特定字符

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

我想extact一審「*」的詞與詞之間(asterik)和「:」(分號),如這裏的結果應該是「WTY」。我已經嘗試過下面的方法,但不能按預期工作。

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

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

+0

你不想使用正則表達式? – Mahi

+0

RegExp會很好,但我無法使用RegExp來完成它,我想要得到star的第一個實例,然後在該開始和冒號 –

+0

之間的字符串,你只能使用正則表達式來解決這個問題。平原js不會幫你 – Mahi

回答

1

使用正則表達式

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

+0

它是否會從一個大字符串中提取,如果有其他多行其他行呢? –

+0

我編輯了答案,以使其與每個匹配項相匹配@BilalHussain – baao

+0

這非常有幫助,但我只希望在第一次發生時只做一次。 –

0

如果你確信你的字符串包含這些符號,在你的功能方面,你可以寫這樣的事情:

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

相關問題