Negative look-ahead always returns true [duplicate] - javascript

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 7 years ago.
I am using this regular expression
/(?!results)/i
As far as I understand, it will match any string that does not contain the word "result". However, when I try
/(?!results)/i.test('basketball results')
it returns true. How do I match strings that do not contain the word results?

This regex matches every position that has no results after it. See demo.
To match an expression that does not contain results, you need to use ^(?!.*results.*$).*. See another demo.

You can use a simple indexOf check here. It will return -1 is a substring is not contained in the string and zero or greater otherwise:
"basketball results".indexOf('results') == -1

Related

how to find last occurrence of a parent in a given below string [duplicate]

This question already has answers here:
Regex Last occurrence?
(7 answers)
Closed 3 months ago.
I have a below pattern string.
'GP0|#92ca7467-4c0d-461a-aac4-2bc8fd9ee16a;L0|#092ca7467-4c0d-461a-aac4-2bc8fd9ee16a|Analysts;GTSet|#7fa22453-62b1-4bec-b73d-01ccf115d558;GPP|#fd613617-ba9d-43e5-9990-95f96f94af2a;GPP|#ba395283-6169-4c6d-84d5-1cecb3c2a73b;GP0|#a547b95c-0dfa-4f42-b540-e55872fb2e81;L0|#0a547b95c-0dfa-4f42-b540-e55872fb2e81|Awards;GP0|#c4363ae8-8608-4309-92f0-5079c69b47e4;L0|#0c4363ae8-8608-4309-92f0-5079c69b47e4|Digital Workplace;GP0|#1976b988-a993-4f13-a1e8-d847138eebc6;L0|#01976b988-a993-4f13-a1e8-d847138eebc6|Intranet;GP0|#a68218df-b9e8-4f07-bfff-22cab83bbc0d;L0|#0a68218df-b9e8-4f07-bfff-22cab83bbc0d|Microsoft;GP0|#57737444-1a1b-4c87-a479-1548b58e44e3;L0|#057737444-1a1b-4c87-a479-1548b58e44e3|Research;'
I want to get last occurrence of the pattern which starts with GPP and ends with ;
expected output: GPP|#ba395283-6169-4c6d-84d5-1cecb3c2a73b;
I tried this regex /GPP\|.+?;/i but it gives the first occurrence i.e. GPP|#fd613617-ba9d-43e5-9990-95f96f94af2a;
As they come after each other, you can match the pattern and assert that there is no following occurrence starting with the same pattern:
As the match ends on a single character, you can match any character except ; instead using a negated character class.
\bGPP\|[^;]+;(?!GPP\|[^;]+;)
Regex demo

How to check the sting is alphanumeric or not in peggyjs [duplicate]

This question already has answers here:
RegEx for Javascript to allow only alphanumeric
(22 answers)
Closed 1 year ago.
I want to check the given string is alphanumeric or not. i.e. the expected output is as follows
123 should retun false
abc should retun false
a123 should retun true
1a23 should retun true
I tried with the ^[a-zA-Z0-9]*$ regex. It is not working as expected. Can anyone suggest the working peggyjs regex? Thanks.
You can assert not only digits, and match at least a single digit restricticting to match to only a-z or a digit.
Using a case insensitive match:
^(?!\d+$)[a-z]*\d[a-z\d]*$
Regex demo
If you know the order (letters then numbers for example) you can do .*[a-zA-Z].*[0-9]
But I assume you can't make such assumptions so I would use the slightly more complex ^(?=.*[a-zA-Z])(?=.*[0-9]).* which means "a letter somewhere later, and also a number somewhere later".
PS : you can replace all [0-9] by \d if you like.
Edit : that's only assuming you don't get other kinds of characters, use Alireza's regex instead if you need to.

Check if first character of string is digit or whitespace with regex or alternative [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to check if a given string's first character is a word character/not a digit or white-space and return a boolean.
I'm trying to use regex for this. What I have is:
function isFirstIndexCharacter(str){
var regex = RegExp('w')
return regex.test(str[0]);
}
I always get false no matter what my input is. Thanks for any help on this.
You can do something like this -
function isFirstIndexCharacter(str){
return /^\w/.test(str[0]);
}

Replacing a string if it contains alphabet from an array in js [duplicate]

This question already has answers here:
Strip all non-numeric characters from string in JavaScript
(12 answers)
Closed 3 years ago.
I have an array like ['adsd','ssd2','3244']. I want to replace a string if it contains any alphabet with '----'. So, the above array should be like ['----','----','3244']. How can I do that? Can I so it with regular expression?
yourArray.map(str => /[a-z]/i.test(str) ? '----' : str)
['adsd','ssd2','3244'].map(function(item) {
return /[a-z]/i.test(item) ? '----' : item;
});
Edit: a bit of explanation about what's happening here.
map() applies a function that transforms every element of the array. More info.
/[a-z]/i is a regex that matches every character in the alphabet. The i makes it case-insensitive, so it matches a and also A.
test checks whether a given string matches the regex. More info.
? '----' : item uses a ternary operator to return either ---- or the original string, depending on whether the string has any alphabetic character. More info.

Simple Regex skips always first character on a line [duplicate]

This question already has answers here:
Regex that can match empty string is breaking the javascript regex engine
(2 answers)
Closed 4 years ago.
I have a very strange effect when using a particular regex in JavaScript. If I use /^|.+/gm, it always skips the first character on a line.
According to regex101.com, it doesn't happen with pcre (php), but does happen in JavaScript, Python, and GoLang. Any ideas on why this could be happening?
In Javascript, empty matches still increment the current index being searched in the string by one. If anything is matched starting at position X in the string, the next match must start at least at position X + 1. (PCRE does not exhibit this behavior; empty matches which don't consume any characters permit an additional non-empty match immediately following that empty match)

Categories

Resources