I am not sure if this is exactly possible. Consider this example set of data:
Mountain Big mountain Mountain of stone A mountain on a hill
I want to match Mountain. Nothing else. No other parts of that set. Just the exact line, Mountain. Everything I've tried either matches for all instances of Mountain or for none of them. Plenty of people want to match an exact word or phrase, but I seem to be the only one who wants to match only one exact word or phrase.
If this CAN be expanded to a phrase that would be perfect. Assume:
Go for a hike Go for a hike, on a mountain. I want to go for a hike.
Where I want to match only "Go for a hike" but no phrase that contains it.
We can match an exact string with JavaScript by using the JavaScript string's match method with a regex pattern that has the delimiters for the start and end of the string with the exact word in between those.
The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character.
Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol. Example: "^a" matches "a" at the start of the string. Example: "[^0-9]" matches any non digit.
Allows the regex to match the word if it appears at the end of a line, with no characters after it. | indicates an “or,” so the regex matches any one of the words in the list. s matches a space character. Use this character to separate words in a phrase.
Plenty of people want to match an exact word or phrase, but I seem to be the only one who wants to match only one exact word or phrase. If this CAN be expanded to a phrase that would be perfect. Assume: Go for a hike Go for a hike, on a mountain.
^ matches the start of a new line. Allows the regex to match the number if it appears at the beginning of a line, with no characters before it. $ matches the end of a line. Allows the regex to match the number if it appears at the end of a line, with no characters after it.
A regular expression that matches a slash at the end of a string. A regular expression that matches the first word after a specific word in a sentence. A regular expression that determines if a given string has all unique (none repeating) characters. A regular expression that matches multiple Email addresses separated by semicolons in a string.
To match the exact line "Mountain" use this: /^Mountain$/
You haven't mentioned what language you're working in, so the exact form of the pattern might have to change.
If you have a string that contains multiple lines, the following two regular expressions may help:
\r\nWORD\r\n|^WORD\r\n|\r\WORD$|^WORD$ \r\nA\sPHRASE\r\n|^A\sPHRASE\r\n|\r\A\sPHRASE$|^A\sPHRASE$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With