Use regex backreference inside character except class [^] [duplicate] - javascript

This question already has answers here:
Negating a backreference in Regular Expressions
(6 answers)
Closed 4 years ago.
Lets take:
stringi = 'xnxx xnnx xnnxn'
My regex is: (n)[^n]
I want to make my regex a little more dynamic like that:
(n)[^\1] -\1 beeing the capt. grp 1
My desired result would be that:
(n)[^\1] would be equal (n)[^n]
(x)[^\1] would be equal (x)[^x]
How can I not match a NOT-\1 character?

using a negative lookahead, the . is to match any character as n length is one
(n)(?!\1).

Related

Regex pattern for URL string [duplicate]

This question already has answers here:
Regex to match only letters
(20 answers)
Closed 3 years ago.
I need to find regex pattern for url and use regex.test() so only string like this:
http://*.margonem.pl/
so it's exactly like above string and where * must appear and can be string which only contains a-z letters without any signs.
That would be http:\/\/[a-z]+\.margonem\.pl\/:
Matches
http://a.margonem.pl/
http://foo.margonem.pl/
Does not match
http://hello-world.margonem.pl/
http://abcq443435u4531.margonem.pl/

JS: check if a single character is NOT a whitespace [duplicate]

This question already has answers here:
regex to match a single character that is anything but a space
(3 answers)
Closed 4 years ago.
How to check in JavaScript if the specified character is not a whitespace using regex only? Right now I am doing something like the code below with negation ! but I would like to avoid mixing of two things to avoid confusions.
if (!/\s/.test(character))
console.log('this is not a whitespace');
if (/\S/.test(character))
console.log('this is not a whitespace');
Use the negated set notation in the regex
/[^\s]/
That will match everything that isnt a whitespace.

Get non-integers from string in javascript [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
How can I create a regex for that returns true if it has only numbers and '+' basically 0-9 & +. Using javascript or jQuery.
Regex for plus anywhere: /^[0-9+]+$/
Regex for plus only infront: /^\+?[0-9]+$/
What it does:
^ Matches the beginning of the string
[0-9+] Matches 0123456789+
+ Matches one or more
$ Matches the end of the string
Other version:
\+? Matches zero or one plus signs in the front
Maybe try regexr for future regex development.
How to test in code:
function isOnlyNumber(str) {
return /^[0-9+]+$/.test(str);
}

Regex in Javascript using OR [duplicate]

This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 6 years ago.
I'm trying to write a regex expression to match multiple characters such as , OR . OR : OR ( OR )
I have this
removePunctuation = /\.$|\,$|\:|\(|\)/;
word = rawList[j].replace(removePunctuation,"")
I just want to remove periods & commas at the end of the sentence but all instances of ( ) :
What am I doing wrong?
You need to add the "g" qualifier if you want to remove all the punctuation.
removePunctuation = /\.$|\,$|\:|\(|\)/g;
I'd go with something like this:
/[.,]+$|[:()]+/g
It'll match periods and commas at the end of a sentence, and brackets and colons everywhere.

Regex - find specific pattern not surrounded by [] [duplicate]

This question already has answers here:
How to search regex only outside curly brackets
(2 answers)
Closed 8 years ago.
In given random string: '#id1 .class1 .1class [price="23 .23 "] .7ytr"
I want to replace the pattern - dots followed by number followed by {n} alpha numeric like: .8acb8
But ignore it when it's surrounded by "[]" like - [price="23 .23 "].
For now I wrote only the pattern I need to find without ignoring []:
str.replace(/\.(\d+\w+)/g, '[class="$1"]');
You're going to use negative lookaheads:
\.\d\w+(?![^[]*\])
Explanations:
\.\d\w+ # Any dot followed by a number followed by alpha-numeric characters
(?![^[]*\]) # Which is not inside brackets (Negative Lookahead)
Live demo
\[[^]]*\]|(\.\d\w+)
Try this.Grab the captures or matches.See demo.
http://regex101.com/r/qU4wM7/1

Categories