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);
Related
This question already has answers here:
Javascript insert space at nth position in string
(3 answers)
How to format a number with commas as thousands separators?
(50 answers)
Closed 2 years ago.
I can do it with this:
const mystring = 'abcdefgh' // or '00000000'
mystring.replace(/(.{3})/g, '$1 ')
But then I get abc def gh.
How to do it from the right and get ab cde fgh ?
Lookahead for groups of 3 characters, eventually followed by the end of the string:
const mystring = '00000000'
console.log(
mystring.replace(/(?=(?:.{3})*$)/g, ' ')
);
This question already has answers here:
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 2 years ago.
I have the following code and want to be able to only return 10 characters after the 'LAST='. If there isn't 10 chars then don't return anything.
const value = 'LAST=uirndheusnrm38f6xxxxxxx'
const m = value.match(/LAST=([^\?&]+)/i)
if (m)
{
console.log('i know it matched LAST= and returned the next 10 chars')
}
Is it possible without doing a substr(0,10) ?
In order to match exactly 10 characters we can use {10}:
const m = value.match(/LAST=([^\?&]{10})/i);
This question already has answers here:
Javascript nth occurance of a string and extract the sub string upto that
(3 answers)
Regex expression to cut string at nth occurrence of character and return first part of string
(3 answers)
Cutting a string at nth occurrence of a character
(5 answers)
Closed 3 years ago.
I want to get a substring that is before the nth point.
For example I have:
let str = "my.string.is.like.that"
Now suppose I want substr= "my.string.is.like" that is all before the 4th point. How can I do that?
You can use split and join,
Second parameter in split is used to limit the number of element to be included in final output
let str = "my.string.is.like.that"
let limited = str.split('.',4).join('.')
console.log(limited)
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);
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,"")