2016-11-19 143 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]

相关问题