This question already has answers here:
How do I remove letters and dashes and dollar signs in a string using a regular expression?
(6 answers)
Closed 8 years ago.
I have phone numbers like
(123)-456-7890
123-456-7890
123 - 456 - 7890
But i need to convert these into 1234567890. how can I do that in Javascript ?
Just strip out unwanted characters by replace. You can still use regex if you need:
numbers = phoneNumber.replace(/[^\d]/g,'');
Edit: If you are interested in capturing the parts of the number, you can use .match():
parts = phoneNumber.match(/(\d{3}).*?(\d{3}).*?(\d{4})/);
And parts will be an array of 4 items:
parts[0] - The captured string, from the first number to the last.
parts[1] - The area code (3 digits as a string)
parts[2] - The prefix (3 digits as a string)
parts[3] - The last 4 digits as a string.
You can then do what you need to like put them into a string with:
parts.shift();
number = parts.join('');
Which will also give you the same answer.
Try something like this:
var onlyNums = "(123)-456-7890".replace(/[^0-9]/g,"")
Related
This question already has answers here:
Convert currency to decimal number JavaScript
(3 answers)
Closed 9 months ago.
How to convert R$ 800,000.00 in string type to 800000 decimal type using JavaScript?
Why don’t you just use
currenyValueInt = parseInt(currencyValueString.split(" ")[1])
You sanitize the string by replacing everything but digits and the decimal separator and then cast it to a Number, either with Number() or by simply adding a + in front of the expression:
let str = +"R$ 800,000.00".replace(/[^\d.]/g, '');
console.log(str, typeof str)
This question already has answers here:
How to disallow consecutive five digits and more using regex?
(2 answers)
Closed 3 years ago.
I am looking for a regex format that would allow alphanumerical values but does not allow two or more integers to be next to each other.
For example:
A1AAAA - is match
123 - not match
111 - not match
159 - not match
A172 - not match
92A - not match
A1A1A1 - is match
Most of what I searched shows either consecutive (123) or identical (111) examples.
I tried
^([a-z0-9A-Z])
but this does not produce the desired output.
Check if /\d{2}/ matches - if it does, then you have two digits side-by-side:
for (const str of ['A1AAAA', '123', '111', '159', 'A172', 'A1A1A1']) {
console.log(!/\d{2}/.test(str));
}
I would use a negative lookahead here:
^(?!.*\d\d)[A-Za-z0-9]*$
var pass = "abc1def7";
var fail = "abc123";
console.log(/^(?!.*\d\d)[A-Za-z0-9]*$/.test(pass));
console.log(/^(?!.*\d\d)[A-Za-z0-9]*$/.test(fail));
This question already has answers here:
How can I cut a string after X characters?
(5 answers)
Closed 4 years ago.
How to remove every character after 9 digits.
I tried the following but is removing the whole word
$(this).val(str.replace(/^.{9,}$/g,""));
You don't need regex to do this. You can use .substr()
Here I used str.substr(0, 9); where 0 is the start of the string you want to keep, and 9 is the length of the portion you want to keep (ie: to the 0+9th index is where it will cut off the string):
let str = "123456789Hello world!";
let res = str.substr(0, 9);
console.log(res);
This question already has answers here:
How to convert a currency string to a double with Javascript?
(23 answers)
Closed 7 years ago.
What I'm talking about is reading a string into a Number, e.g.
"$107,140,946" ---> 107140946
"$9.99" ---> 9.99
Is there a better way than
dolstr.replace('$','');
dolstr.replace(',','');
var num = parseInt(dolstr,10);
???
Using regex is much simpler to read and maintain
parseFloat(dolstr.replace(/\$|,/g, ""));
You can just put all of this in oneliner:
parseFloat(dolstr.replace('$','').split(",").join(""))
Notice that I do not replace the second one, because this will remove just the first ','.
Using a simple regex and the string's replace function
parseFloat(dolstr.replace(/[^\d\.]/g, ''))
Breakdown
It replaces every instance of a character that is not a digit (0 - 9) and not a period. Note that the period must be escaped with a backwards slash.
You then need to wrap the function in parseFloat to convert from a string to a float.
Assuming input is always correct, just keep only digits (\d) and the dot (\.) and get rid of other characters. Then run parseFloat on the result.
parseFloat(dolstr.replace(/[^\d\.]/g, ''))
This question already has answers here:
JavaScript regex for alphanumeric string with length of 3-5 chars
(3 answers)
Closed 7 years ago.
I need REGEXP for alpha Numeric zip code, which contains minimum 3 & maximum 10 values.
Invalid inputs are: AAAAA, A1, AA, 12, 2A
Valid inputs are: 123456, 123, A1234, A12, A12A, A9A
This is the regex I'm currently using:
/(^[A-z0-9]\d{3,10})+$/
It doesn't allow to specify only digits like this 12345 but input like A123 matches correctly.
The question is not completely clear. If you mean that you can use between 3 and 10 characters, and these characters can be alphanumerical characters (digits and [A-Za-z]), you can use:
/^(?=.*\d.*)[A-Za-z0-9]{3,10}$/
regex101 demo.
The regex works as follows:
^[A-Za-z0-9]{3,10}$ says the regex consists out of 3 to 10 characters that can be digits and/or A-Z/a-z.
The lookahead (?=.*\d.*) enfoces that the string contains at least one digit somewhere in the string.