我有这个字符串:
λ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/
关于javascript - 获取最后一个 "."之后可能包含 "."的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31083242/