[英]Regex, how do I say, match everything but one word?
Using regex I'm trying to match anything BUT one word, that word being '/item/'. 我正在使用正则表达式尝试匹配任何一个单词,但该单词为'/ item /'。
([^/]+)
That matches anything (true), but I'd like it to be false if the word '/item/' is in there. 可以匹配任何东西(真),但是如果其中有“ / item /”这个词,我希望它是假的。
Would I have to do something like this? 我需要做这样的事情吗?
([^/]+|!/item/)
Where the pipe is 'OR' and the ! 管道是“或”的地方! is 'is not,' this example that I've written is definitely the wrong syntax... I'm a newb when it comes to regex. 是“不是”,我写的这个示例绝对是错误的语法...关于正则表达式,我是个新手。
Update: 更新:
Here is the live example: 这是现场示例:
^(category)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$
category/all-weather-wicker/bermuda/ table /2977/ should pass 类别/全天候柳条/百慕大/ 表 / 2977 / 应该通过
category/all-weather-wicker/bermuda/ item /2977/ should fail 类别/全天候柳条/百慕大/ 项目 / 2977 / 应该会失败
Thanks for your help! 谢谢你的帮助!
Try this: 尝试这个:
var subject = "category/all-weather-wicker/bermuda/item/bermuda-end-table/table/2977/";
if (/^category(?!.*?\/item\/\d+\/).*?$/im.test(subject)) {
console.log("passed");
} else {
console.log("failed");
}
EXPLANATION: 说明:
Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed, line feed, line separator, paragraph separator) «^»
Match the character string “category” literally (case insensitive) «category»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!.*?/item/\d+/)»
Match any single character that is NOT a line break character (line feed, carriage return, line separator, paragraph separator) «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character string “/item/” literally (case insensitive) «/item/»
Match a single character that is a “digit” (ASCII 0–9 only) «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “/” literally «/»
Match any single character that is NOT a line break character (line feed, carriage return, line separator, paragraph separator) «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed, line feed, line separator, paragraph separator) «$»
Hi you can try the reverse logic, might be easier to look for the existance of '/item/' 嗨,您可以尝试反向逻辑,可能更容易查找'/ item /'的存在。
javascript... JavaScript的...
var st1 = 'here is a string with /item/ in it';
var m = st1.match(/\/\bitem\b\//g);
m = (m !== null)?false:true;
console.log(m);
NOTE: don't forget to escape your "/" like "\\/" 注意:不要忘了像“ \\ /”一样转义您的“ /”
here is a fiddle to demonstrate... 这是一个小提琴来演示...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.