我正在检查字符串数组中是否有特定的模式组合。我在同时使用 Meteor 的 Match 函数和正则表达式文字时遇到问题。我想检查数组中的第二个字符串是否是 url。
addCheck = function(line) {
var firstString = _.first(line);
var secondString = _.indexOf(line, 1);
console.log(secondString);
var urlRegEx = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g;
if ( firstString == "+" && Match.test(secondString, urlRegEx) === true ) {
console.log( "detected: + | line = " + line )
} else {
// do stuff if we don't detect a
console.log( "line = " + line );
}
}
如有任何帮助,我们将不胜感激。
最佳答案
Match.test用于测试变量的结构。例如:“它是一个字符串数组,或者一个包含字段createdAt的对象”,等等。
RegExp.test另一方面,用于测试给定字符串是否与正则表达式匹配。这看起来就是你想要的。
尝试这样的事情:
if ((firstString === '+') && urlRegEx.test(secondString)) {
...
}
关于regex - 使用 Meteor Match 和 Regex 检查字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140527/