I'm trying to find the Javascript regex to detect a keyword (any character) followed by either new line or end of string. This is my attempt:
.+(?:\n|$)
I'm not sure that the $
can be used in the or condition. Any thoughts?
UPDATE
What I am trying to detect with the regex is a keyword follow by either (1) another keyword, or (2) nothing (therefore new line or end of string)
For example: the string "aaa" right before the new line it would be a match; the string "bbb" at the end of the string it would be a match as well.
End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.
The correct regex to use is ^\d+$. Because “start of string” must be matched before the match of \d+, and “end of string” must be matched right after it, the entire string must consist of digits for ^\d+$ to be able to match.
To match the start or the end of a line, we use the following anchors: Caret (^) matches the position before the first character in the string. Dollar ($) matches the position right after the last character in the string.
RegExp. prototype. global has the value true if the g flag was used; otherwise, false . The g flag indicates that the regular expression should be tested against all possible matches in a string.
Here you go: https://regex101.com/r/tP5eV8/2
/[a-z]+$/gm
g returns multiple matches and m is for multi line
$ matches new line
This only matches the last keyword of the line. Here is one that matches one or several words separated by space and ending in a newline: https://regex101.com/r/zB9bS0/1
(?:(?: |^)([a-z]+))+$
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