获取最后一个“.”之后的字符串可能包含“。”

Get string after last "." that may contain "."

我有这个字符串:

λx.λy.Math.pow(x,y)

我想得到:

Math.pow(x,y)

基本上最后一个 . 之后的所有内容都带有 λ。最后一部分还可能包含 λ.

λx.λy.print("λ"+x+".λ"+y)

以下正则表达式应该有效:

/((λ.+?)\.)+([^λ].*)/

正则表达式期望以 λ 开头的单词序列,由 . 分隔,直到找到不以 λ 开头的单词。当找到该词时,最后一组匹配 - 您要查找的组。

示例:

var
    re = /((λ.+?)\.)+([^λ].*)/,
    m,
    test1 = 'λx.λy.Math.pow(x,y)',
    test2 = 'λx.λy.print("λ"+x+".λ"+y)',
    test3 = 'λx.λy.λx.λy.λx.λfoo.λa.λz.print("λ"+x+".λ"+y)';

console.info(test1.match(re).pop()); // prints 'Math.pow(x,y)'
console.info(test2.match(re).pop()); // prints 'print("λ"+x+".λ"+y)'
console.info(test3.match(re).pop()); // prints 'print("λ"+x+".λ"+y)'

你应该总是寻找最后一组。当然,你应该先检查匹配:

var
    re = /((λ.+?)\.)+([^λ].*)/,
    m,
    test4 = "won't match";

m = test4.match(re);

if (m) {
    console.info(m.pop());
} else {
    console.info('No match found');
}

看到它在这里工作:https://jsfiddle.net/luciopaiva/0ty4z2kb/