I have small requirement.I want to search a string with exact match.
Suppose i want to search for None_1, i am searching for 'None_1' using /None_1/, but it is matching even "xxxNone" but my requirement is it should match only None_[any digit].
Here is my code
/^None_+[0-9]{?}/
So it should match only None_1 , None_2
You should also anchor the expression at the end of the line. But that alone will not make it work. Your expression is wrong. I think it should be:
/^None_[0-9]+$/
^ matches the beginning of a line
[0-9]+ matches one or more digits
None_ matches None_
$ matches the end of a line
If you only want to match one digit, remove the +.
Your original expression /^None_+[0-9]{?}/ worked like this:
^ matches the beginning of a line
None matches None
_+ matches one or more underscores
[0-9] matches one digit
{? matches an optional opening bracket {
} matches }
Try this:
/^None_+[0-9]{?}$/
Related
I need a regular expression that matches the following conditions:
logger(textthatdoesnotcontain|)
Example:
logger(sample log) // Match
logger(sample log | test) // Not Match
I have tried below regex, but not working:
logger(?!*\|.*)
logger\([^\|]+\) should do the trick
Or if you want to match whole lines
^logger\([^\|]+\)$
This accept any character except "|" between "logger(" and ")"
If you want your line to not match if it contains |, you can just use this regex,
^logger\([^|]*$
Demo
You don't need a negative look ahead when you want to fail the match just because of one character and can use negated character set for such use cases like this [^|]
I'am looking to exclude matches that contain a specific word or phrase. For example, how could I match only lines 1 and 3? the \b word boundary does not work intuitively like I expected.
foo.js # match
foo_test.js # do not match
foo.ts # match
fun_tset.js # match
fun_tset_test.ts # do not match
UPDATE
What I want to exclude is strings ending explicitly with _test before the extension. At first I had something like [^_test], but that also excludes any combination of those characters (like line 3).
Regex: ^(?!.*_test\.).*$
Working examples: https://regex101.com/r/HdGom7/1
Why it works: uses negative lookahead to check if _test. exists somewhere in the string, and if so doesn't match it.
Adding to #pretzelhammer's answer, it looks like you want to grab strings that are file names ending in ts or js:
^(?!.*_test)(.*\.[jt]s)
The expression in the first parentheses is a negative lookahead that excludes any strings with _test, the second parentheses matches any strings that end in a period, followed by [jt] (j or t), followed by s.
I have this regular expression:
/([a-záäéěíýóôöúüůĺľŕřčšťžňď])-$\s*/gmi
This regex selects č- from my text:
sme! a Želiezovce 2015: Spoloíč-
ne pre Európu. Oslávili aj 940.
But I want to select only - (without č) (if some character from the list [a-záäéěíýóôöúüůĺľŕřčšťžňď] is before the -).
In other languages you would use a lookbehind
/(?<=[a-záäéěíýóôöúüůĺľŕřčšťžňď])-$\s*/gmi
This matches -$\s* only if it's preceded by one of the characters in the list.
However, Javascript doesn't have lookbehind, so the workaround is to use a capturing group for the part of the regular expression after it.
var match = /[a-záäéěíýóôöúüůĺľŕřčšťžňď](-$\s*)/gmi.match(string);
When you use this, match[1] will contain the part of the string beginning with the hyphen.
First, in regex everything you put in parenthesis will be broken down in the matching process, so that the matches array will contain the full matching string at it's 0 position, followed by all of the regex's parenthesis from left to right.
/[a-záäéěíýóôöúüůĺľŕřčšťžňď](-)$\s*/gmi
Would have returned the following matches for you string: ["č-", "-"] so you can extract the specific data you need from your match.
Also, the $ character indicates in regex the end of the line and you are using the multiline flag, so technically this part \s* is just being ignored as nothing can appear in a line after the end of it.
The correct regex should be /[a-záäéěíýóôöúüůĺľŕřčšťžňď](-)$/gmi
I'm trying to get the last character of a string, but only if it matches the following RegEx:
/\W/
If it doesn't match, I want it to move to the next last character and do the test again until it finds a match.
function getLastChar(s) {
var l = s.length - 1;
return s[l - i]; // need logic to keep checking for /\W/
}
getLastChar('hello.'); // returns '.', want it to return 'o'
I have the following idea of how to match if the character isn't a letter/number; however, I'm searching for a more elegant solution, one that would allow me to return the last matching character on a single line with a ternary if()
if(string.match(/\W/) !== null){
//keep looking for a match, going backwards.
}
/(\w)\W*$/
Capture one \w character, that is followed by zero or more \W characters, anchored to the end of the subject.
[Edited after comments.]
Easy enough.. just do a greedy match up to the last \W
string.match(/.*(\W)/)
If you're looking for a simple answer, you might be able to accomplish it with a single regex, no looping required - something like the following:
^.*(\W)[^\W]*$
The capture group will have the last non-word character.
For example, running this regex on ~~~~99*9 puts the character * in the capture group.
Edit:
However, after re-reading your question, it seems like you really meant to use \w not \W - in other words, you want the last word character, not the last non-word character. That's easily fixed by swapping \W for \w in the regex above.
The code is showed as follows:
alert(/symbol([.\n]+?)symbol/gi.test('symbolbbbbsymbol'));
or
alert(/#([.\n]+?)#/gi.test('#bbbb#'));
Because you are looking for dots with a character class inside of < and >. Remove the character class:
/<(.+?)>/
Clarification after question edit:
First code block should be using this pattern: /symbol(.+?)symbol/
Second code block should be using this pattern: /#(.+?)#/
The regex returns false because a dot loses its special power to match any character (but newlines) when placed within a character class [] - it only matches a simple ".".
To match and capture the substring delimited at either end by the same single character, the most efficient pattern to use is
/#([^#]+)#/
To match and capture the substring delimited at either end by the same sequence of characters, the pattern to use is
/symbol(.+?)symbol/
or, if you want to match across newlines
/symbol([\s\S]+?)symbol/
where [\s\S] matches any space or non-space character, which equates to any character.
The ? is inlcuded to make the pattern match lazily, i.e. to make sure the match ends on the first occurence of "symbol".