Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a string that has some numbers in the middle of the string.
For example,
var str = "abcd-123456.com"
I want to remove the numbers like this
abcd.com
I am not trying to replace all numbers.
I have to replace only -*. expression with "".
How do I do this in JavaScript?
var str = "abcd-123456.com"
str = str.replace(/-[0-9]*/g, '')
Your comments lead me to believe you want
var str = "abcd-123456.com";
var str1 = str.substring(0,str.indexOf("-"))+str.substring(str.indexOf("."))
console.log(str1);
//or with regex
// dasah plus 6 digits to nothing
var str2 = str.replace(/-\d{6}/,"")
console.log(str2);
// dash, digits and an dot to dot
var str3 = str.replace(/-\d+\./,".")
console.log(str3);
AS PER YOUR COMMENT
The answer of Shivaji Varma will nearly do the tricks.
var str = "abc5d-123456.c0om"
str = str.replace(/-[0-9]*./g, "")
console.log(str)
Soooo,
Using the slash indicate a regular expression.
[0-9] indicate you want to replace all digit between 0 and 9 (included)
"*" to remove all digits
"-" and "." to delimite
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have a string that represents a fraction and I want to extract the numerator and denominator.
var stringFraction = '99/99 (100%)';
var fraction = stringFraction.split(' ')[0].split('/');
console.log(0, Number(fraction[0]));
console.log(1, Number(fraction[1]));
This works fine but I'm wondering if a regex would be better?
Using a regex, you would be able to go for a simple match/find.
In your case, you first split the fraction itself from the remaining part, to then split again on the '/'.
In other words: a regex would allow you to reduce your code to a single match operation.
See here for some guidance how that would work.
Of course, you could also do that specific "matching" in a more manual mode:
get the string from 0 to index-1 of '/'
get the string from '/' to ' '
In other words, there are plenty of ways to retrieve that information. Each one has different pros and cons, and the real answer for a newbie learning this stuff: make experiments, and try them all.
There is no reason you can't do trimming and all with a single regex
without having to go through the gyrations with split.
Try this
/0*(\d+)\s*\/\s*0*(\d+)/
Formatted
0*
( \d+ ) # (1)
\s* / \s*
0*
( \d+ ) # (2)
JS sample
var strSample =
"0039/99 (100%)\n" +
"00/000 (100%)\n" +
"junk 102 / 487\n";
var NumerDenomRx = new RegExp( "0*(\\d+)\\s*/\\s*0*(\\d+)", "g");
var match;
while ( match=NumerDenomRx.exec( strSample ))
{
console.log("numerator = ", match[1] );
console.log("denominator = ", match[2] );
console.log( "-------------------------" );
}
If all strings have the same pattern
var stringFraction = '99/99 (100%)';
var fraction = stringFraction.match(/\d+/g); // result = ["99", "99", "100"];
Now technically this is shorter than spliting it, 15 vs 26 letters/signs/spaces, but only if the length of the array doesn't bother you. Otherwise you will have to chain extra method
.slice(1,-1)
that's +12 extra signs/letters. If the string is more complex
var fraction = stringFraction.match(/\d+\/\d+/)[0].split('/');
There are endless variations how to solve it really
P.S. Unless you got more complex strings, regex is not needed.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm wondering how to show the first two characters and replace all last character of a string by symbol *.
Ex: 121,121,121 -> 12x,xxx,xxx .
Thanks
I love using regex when it comes to replace string according to some pattern.
var p = '121,121,121';
var regex = /(?<=.{2})([0-9])/gm;
console.log(p.replace(regex, 'x'));
You can use substring and regular expression. See the sample below.
var str = "121,121,121";
var res = str.substring(0, 2) + '' + str.substring(2, str.length).replace(/[0-9]/g,"x");
alert(res);
Just use substring and replace with a simple regex (to single out digits and keep commas and other punctuation):
const str = "121,121,121";
const obfuscated = `${str.substring(0, 2)}${str.substring(2).replace(/\d/g, "*")}`;
console.log(obfuscated);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
how do you replace a string value with another in JavaScript ?
I have a string that contains a lot of this substring- Items[0]. How do I replace all of this to Items[1], taking into account that '0' is a dynamic value that I get from a variable.
I have used the replace method, and tried some 'RegExp' but couldn't do that. I think its because of those square brackets:
str.replace(new RegExp("Items[" + j + "]", "g"), "Items[" + (j - 1) +"]"));
Any help is appreciated. Thanks :)
You use new RegExp to create the regular expression. In the regex, you have to use \ to escape the [ because otherwise it has a special meaning. Since you're going to have to use a string to create the regex (since you need to include the value of your variable), you also have to escape the \ in the string, so we end up with two:
var variable = 0;
var rex = new RegExp("\\[" + variable + "]", "g");
var str = "This has Item[0] in more than one Item[0] place.";
var str = str.replace(rex, "Item[" + (variable + 1) + "]");
// If you want a dynamic replacement ^^^^^^^^^^^^^^^^^^^
// In this case, I used the value plus one.
console.log(str);
The "g" is a flag meaning "global" (throughout the string, not just the first occurrence).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I find and extract capitalized words of a string with regex?
I would like to:
extract the capitalized words of a string, as an array
extract the last capitalized word of a string, as a substring:
Both with one regex
If I have this:
var str="This is a STRING of WORDS to search";
I would like to get this 1:
allCapWords // = ["STRING", "WORDS"]
and 2:
lastCapWord // = "WORDS"
To extract the words into an array:
var allCapWords = str.match(/\b[A-Z]+\b/g);
-> ["STRING", "WORDS"]
(Here's a Regex101 test with your string.)
To pull the last word:
var lastCapWord = allCapWords[allCapWords.length - 1];
-> "WORDS"
var str="This is a STRING of WORDS to search";
var regObj = /\b([A-Z]+)\b/g;
allCapWords = str.match(regObj);
You can try this regexpr /\b[A-Z]+\b/gor \b[A-Z0-9]+\b/g if you are interested in catch numbers inside the string
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I add a character after the second characters in string?
Ex: I want this : 1700 to become this: 17:00.
The simplest way, in my opinion, is using substr to split the string and concatenate the other character in between. This will work no matter the length of the second part:
var str = "1700";
str.substr(0,2)+":"+str.substr(2);
this would do the magic:
"1700".replace(/(..)$/, ":$1")
alternative you can do something like look from behind in substr too like:
var string = "1700";
string = string.substr(0, string.length -2) + ":" + string.substr(-2, 2);
they both also work on something like: 900 wich will turn into 9:00
the regex and the substr line both do the same. if you want readability i'd consider the regex.
You could do this:
'1700'.match(/../g).join(':')
The following regexp accepts 3+ chars:
'700'.match(/^(.+)(..)$/).slice(1, 3).join(':') // "7:00"
Shortest and probably fastest solution:
s[0]+s[1]+":"+s[2]+s[3]
Without using regexp, you can use substr --
string = string.substr(0,2) + ":" + string.substr(2);
You can use substring. Consider the following:
string1 = "1700";
string2 = ":";
string1 = string1.substring(0,2) + string2 + string1.substring(2);
alert(string1);