Regex to capture digits after pattern [duplicate] - javascript

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.

Related

Regex for replacing a particular occurrence of a String in Javascript [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 1 year ago.
For some weird reason, I can't seem to figure out how to solve my current issue.
I have a string that contains this string /partners: and I wish to remove all occurrences of /partners: from the string
I have tried the below code
retrievedData = retrievedData
.replace("/\/partners:", "")
But for some weird reason it is not working.
Any help would be greatly appreciated.

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.

Replacing a special character in a string [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 3 years ago.
I have a simple string like this, let string = "123½"; and I want to replace it.
I've already tried string.replace("½","0.5")
I'm not exactly what is happening here, but maybe it's because it's a special character?
string.replace doesn't modify a string; it returns a new string. Try:
const replaced = string.replace("½","0.5");

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?

Javascript regex to replace only numbers [duplicate]

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

Categories