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

时间:2016-11-19 18:26:20

标签: javascript regex

我有一个如下所示的结果字符串

class AboutAction extends AbstractAction {
    private MyMain myMain;

    public AboutAction(String name, int mnemonic, MyMain myMain) {
        super(name);
        putValue(MNEMONIC_KEY, mnemonic);
        this.myMain = myMain;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (myMain != null) {
            myMain.showPanel(MyMain.ABOUT_PANEL);
        }
    }
}

我想在第一个实例" *"(asterik)和":"(分号)之间取词,这里的结果应该是b" WTY &#34 ;.我试过在下面做,但它没有按预期工作。

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

任何人都可以指出错误或任何其他更好的解决方案吗?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

如果您确定您的字符串包含这些符号,就您的功能而言,您可以这样写:

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

相关问题