Get non-integers from string in javascript [duplicate] - javascript

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);
}

Related

Remove Letters and special characters [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
Hi I'm trying to remove the letters and special characters from javascript.
Example:
var id = "Parameter[0].Category"
I only need the "0" from this. Thank you
Try this
var numbers = id.match(/\d+/)[0];
console.log(numbers);
In general this is called filtering using regular expressions.
If you want to filter out letters and special characters you can use regular expression in JS, like this:
id = id.replace(/\D/g, "")
You replace every (g option) character in your string that is not a digit \D with blank ""

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

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).

Regex in JavaScript acting differently than elsewhere [duplicate]

This question already has answers here:
How can I match a whole word in JavaScript?
(4 answers)
Closed 5 years ago.
I am trying to match the word ethane (preceded with nothing) while avoiding methane. I've tried this in an online regex tester: /(?<!m)ethane/i (which works), but I get an invalid regex expression error in JavaScript. What am I doing wrong?
You can use RegExp /\bethane\b/ to match "ethane" and not "methane"
var thanes = ["ethane", "methane"];
var re = /\bethane\b/;
thanes.forEach(word => console.log(re.test(word)));
See
Difference between \b and \B in regex
How does \b work when using regular expressions?

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

A regular expression that matches strings that don't start with a set of words [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
I have a regular expression that matches strings that start with either the word "db" or the word "admin":
/^\/(db|admin)\//
I'm refactoring my code and my requirements have changed: Now I need the opposite, i.e. a regular expression that matches strings that don't start with one of those two words. Is this possible with regular expressions?
Note: I cannot use JS API - the regular expression is inserted in Express.js's app.all(path, callback) method directly (as the path).
Thanks to Nico for pointing out that JavaScript RegExp has (?!) functionality. The solution seems to be:
/^\/(?!admin|db)/

Categories