If your strings will always be consistent, you could use a Negative Lookahead here.
^(category)/([^/]+)/([^/]+)/((?:(?!\bitem\b).)+)/([^/]+)/?$
See live demo
質問
Using regex I'm trying to match anything BUT one word, that word being '/item/'.
([^/]+)
That matches anything (true), but I'd like it to be false if the word '/item/' is in there.
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
category/all-weather-wicker/bermuda/item/2977/ should fail
Thanks for your help!
解決
If your strings will always be consistent, you could use a Negative Lookahead here.
^(category)/([^/]+)/([^/]+)/((?:(?!\bitem\b).)+)/([^/]+)/?$
See live demo
他のヒント
Hi you can try the reverse logic, might be easier to look for the existance of '/item/'
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...
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) «$»