Regular Expression not containing - javascript

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 [^|]

Related

Regular expression that matches a single word in any order

After a long, unsuccessful search I am starting to wonder if what I am looking for is possible, I would like a regular expression which requires that each letter chosen is mandatory but only once and in any order.
Example : ^[abc]{3}$
The result I expect would be that it matches only that :
abc, bac, cba, acb
While I get :
acc, abb, cca, aab
Do you see where I am going with this?
You may use a regex like this with a negative lookahead of the matched character in a back-reference:
^(?:([abc])(?!.*\1)){3}$
RegEx Demo
Here is another way.
^(?!.*([abc]).*\1)[abc]{3}$
Demo
The negative lookahead
(?!.*([abc]).*\1)
asserts that no character is repeated and
[abc]{3}
together with the two anchors asserts that the string has a length of three and is composed of the characters in the character class.

check every occurrence of a special character followed by a whitespace using regex

I'm trying to check for every occurrence that a string has an # at the beginning of a string.
So something like this works for only one string occurance
const comment = "#barnowl is cool"
const regex = /#[a-z]/i;
if (comment.charAt(0).includes("#")) {
if (regex.test(comment)) {
// do something
console.log('test passeed')
} else {
// do something else
}
} else {
// do other
}
but....
What if you have a textarea and a user uses the # multiple times to reference another user this test will no longer work because charAt(0) is looking for the first character in a string.
What regex test is doable in a situation where you have to check the occurrence of a # followed by a space. I know i can ditch charAt(0) and use comment.includes("#") but i want to use a regex pattern to check if there is space after wards
So if user does #username followed by a space after words, the regex should pass.
Doing this \s doesn't seem to make the test pass
const regex = /#[a-z]\s/i; // shouldn't this check for white space after a letter ?
demo:
https://jsbin.com/riraluxape/edit?js,console
I think your expression is very close. There are two things that are missing:
The [a-z] match is only looking for one character, so in order to look for multiple characters it needs to be [a-z]+.
The flags section is missing the g modifier, which enables the expression to look through the entire text string instead of just the first match.
I believe the regular expression declaration should be adjusted to the following:
const regex = /#[a-z]+\s/ig;
Is this what you want? Matching all the occurrences of the mention?
const regex = /#\w+/ig
I used the \w flag here which matches any word character.
To check for multiple matches instead of only the first one, append g to the regex:
const regex = /#[a-z]*\s/ig;
Your regex with \s actually works, see: https://regex101.com/r/gyMyvB/1

javascript regex with [x|y|z|xy|yz]

I have a regex:
\[[^\[\]]*\]\s*[<>|<=|>=|=|>|<]\s*'?"?\w*'?"?
this basically just parse a equation like:
[household_roster_relationships_topersona_nameadditionalpersono] = "1"
it works good, with
'=','>','<'.
but when the equation has
'<=','>=','<>'.
the parse stop at first character of
'<=','>=','<>'.
I have created a demo on regex101
How can I correct regex so it will work in this situation?
Just change your char class for an alternation:
\[[^\[\]]*\]\s*(<>|<=|>=|=|>|<)\s*'?"?\w*'?"?
^ ^
See demo
You need to replace the character class with a grouping construct.
Use
\[[^[\]]*]\s*(?:<>|[<>]=|[=><])\s*['"]?\w*['"]?
^^^ ^
See the regex demo. The (?:<>|[<>]=|[=><]) non-capturing group (only used for grouping subpatterns) matches either <>, <=, >=, =, > or <.
Note I reduced some alternative branches to make the pattern a bit more compact. Also, I think you just want to match either ' or " at the end, so, a mere ['"]? (1 or 0 ' or ") should be enough.
Also, you do not need to escape a [ inside a character class ([[] matches a single [) and you do not need to escape ] outside a character class, it matches a literal ] symbol.
You need to use () instead of [] for multiple word character class selection ...see updated regex

Using regular expression in Javascript

I need to check whether information entered are 3 character long, first one should be 0-9 second A-Z and third 0-9 again.
I have written pattern as below:
var pattern = `'^[A-Z]+[0-9]+[A-Z]$'`;
var valid = str.match(pattern);
I got confused with usage of regex for selecting, matching and replacing.
In this case, does[A-Z] check only one character or whole string ?
Does + separate(split?) out characters?
1) + matches one or more. You want exactly one
2) declare your pattern as a REGEX literal, inside forward slashes
With these two points in mind, your pattern should be
/^[A-Z][0-9][A-Z]$/
Note also you can make the pattern slightly shorter by replacing [0-9] with the \d shortcut (matches any numerical character).
3) Optionally, add the case-insensitive i flag after the final trailing slash if you want to allow either case.
4) If you want to merely test a string matches a pattern, rather than retrieve a match from it, use test(), not match() - it's more efficient.
var valid = pattern.test(str); //true or false
+ means one or more characters so a possible String would be ABCD1234EF or A3B, invalid is 3B or A 6B
This is the regex you need :
^[0-9][A-Z][0-9]$
In this case, does[A-Z] check only one character or whole string ?
It's just check 1 char but a char can be many times in a string..
you should add ^ and $ in order to match the whole string like I did.
Does + separate(split?) out characters?
no.
+ sign just shows that a chars can repeat 1+ times.
"+" means one or more. In your case you should use exact quantity match:
/^\w{1}\d{1}\w{1}$/

How to match with an exact string using regular expression

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]{?}$/

Categories

Resources