Javascript regex to replace only numbers [duplicate] - javascript

This question already has answers here:
Regex to check whether a string contains only numbers [duplicate]
(21 answers)
Closed 4 years ago.
I want a Javascript regex to replace only numbers i.e. all others alphabets and special characters are allowed.

This should do:
let string= "26kgsl5"
let newString = string.replace(/[0-9]/g, "");
console.log(newString);

Related

Regex to capture digits after pattern [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Why do regex constructors need to be double escaped?
(5 answers)
Closed 2 years ago.
I can't seem to find the issue with this code:
const regex = "/tel:\+61(\d+)/g";
const subscriberNumber = senderAddress.match(regex);
For input text, senderAddress = tel:+619123456789 the subscriberNumber is null.
What's causing it to return null?
You haven't escaped the + in 61. + is used as a concatenation operator too.
I don’t think the parentheses around \d+ are necessary.

RegExp with variable in a string format [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 2 years ago.
I created a RegExp that works as expected however I can't get it to work in a string format
var regex = new RegExp(/IN1\|2.*\r/g);
The regex is supposed to match the line number which will be taken from variable, in above example it would be line number 2. My question is how do I get it to a string format with a variable inside it?
I tried following but it just doesn't work: "IN1\|" + lineNumber + ".*\x0d//g"
Below is a text in case anyone wants to try:
IN1|**1**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**2**||QQ|Noth||||||||20190413|20190413||Self\r
IN1|**3**||QQ|LHS||||||||20200506|""||Private|||||||||||||||||||||2342344\r
Thank you.

split with regex is not returning correct repsonse [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regex to first occurrence only? [duplicate]
(4 answers)
Closed 4 years ago.
I have a string
var str3 = "[a,b,c] there [s,b,c] how are u";
What i want is [there, how are u] but I am getting ["", "how are u"], when splitting with str3.split(/\[.*\]/);
Any idea how i shud do it?
You shouldn't be greedy:
str3.split(/\[.*?\]\s*/);
:-)

Javascript get string occurrences [duplicate]

This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Regex created via new RegExp(myString) not working (backslashes)
(1 answer)
Closed 4 years ago.
I'm trying to return how many times the string s.t() was found in a string, but I can't get the correct regex for this...
For example
var string = 'function(test) {s.t(); s.t(dsabf); s.t();}'
var re = new RegExp('s\.t\(\)', "g");
return re;
should return an array of 2 elements ['s.t()', 's.t()'] but instead it has 3 elements ['s.t', 's.t', 's.t']
I've also tried with ^s\t\(\)$ but this returns no match...
How can I fix my regex in order to make this work as expected?

How to negative match regex in JavaScript string replace? [duplicate]

This question already has answers here:
Strip all non-numeric characters from string in JavaScript
(12 answers)
Closed 8 years ago.
I am trying to replace with a blank all characters except numbers, but this doesn't works:
s.replace(/(?!\d)/g, '')
Thanks!
Use negative character classes:
s.replace(/[^0-9]+/g, '')
or s.replace(/[^\d]+/g, '')
or s.replace(/\D+/g, '')

Categories