I have a string like the following,
var str = "abcd-12ad3dgs4g56.com"
I want like the following from this
abcd.com
I have to replace only -*. expression with ..
How do I do this in JavaScript?
Simply try this
str.replace( /-\w+/, "" ); //"abcd.com"
var str = "abcd-12ad3dgs4g56.com"
console.log(str.replace(/-\w+/, ""));
You could use a regular expression with a positive lookahead.
var str = "abcd-12ad3dgs4g56.com";
console.log(str.replace(/-.*(?=\.)/g, ''));
Related
I have a string [TEST][NO CHANGE][TEST][NOW][TEST] in which [TEST] should be replace with 'replaced', and the result should be replaced[NO CHANGE]replaced[NOW]replaced.
I have Tried the following ways, nothing worked.
1. str.replace(/'[TEST]'/g, 'replaced');
2. str.replace(/[TEST]/g, 'replaced');
3. str.replace('/[TEST]/g', 'replaced');
var str = "[TEST][NO CHANGE][TEST][NOW][TEST]";
var resultStr = str.replace(/'[TEST]'/g, 'replaced');
Actual String: [TEST][NO CHANGE][TEST][NOW][TEST]
After Replacing: replaced[NO CHANGE]replaced[NOW]replaced
[] has a special meaning in regex, which means character class, if you want to match [] you need to escape them
var str = "[TEST][NO CHANGE][TEST][NOW][TEST]";
var resultStr = str.replace(/\[TEST\]/g, 'replaced');
console.log(resultStr)
Try to update using Below snippet.
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
var str = "[TEST][NO CHANGE][TEST][NOW][TEST]";
var result = str.replaceAll('\[TEST\]','replaced')
console.log(result);
replaced[NO CHANGE]replaced[NOW]replaced
Src : How to replace all occurrences of a string in JavaScript
Your regular expression in replace is looking for the string '[TEST]' surrounded by those single quotes and is looking to match any of the characters in TEST because you didn't escape the brackets. Try this regular expression instead:
var resultStr = str.replace(/\[TEST\]/g, 'replaced');
So I was wondering how I would go about splitting a String on specific characters/conditions using Regular Expressions like the following:
On digits
On Letters
On digits following a caret
Here could be an example:
var str1 = "62y^2";
Would return as an array:
[62,y,^2]
Thanks for the help.
You can use String.match() instead of split with the following regular expression (Regex101):
var str="62y^2ad23^123";
var result = str.match(/\^?\d+|[a-z]+/gi);
console.log(result);
You may try the following approach:
var str="62y^2ad23^123";
console.log(str.split(/(\^\d+|[a-zA-Z]+|\d+)/).filter(function(n){ return n != "" }));
Try the below regex
var str = "62y^2";
str.match(/\^(\d+|[a-zA-Z]+)|[a-zA-Z]+|\d+/g); // ["62", "y", "^2"]
I have an string as:
0123456789,, 0987654213 ,, 0987334213,, ......
How can I convert this into
0123456789, 0987654213, 0987334213, ......
i.e I want to remove the second comma
You can do it very simply, like this using regex.
var str = "0123456789,,0987654213,,0987334213,,,,,9874578";
str=str.replace(/,*,/g,',');
console.log(str)
var str = "0123456789,, 0987654213 ,, 0987334213,, ......"
console.log(str.replace(/\,+/g,","));
This will replace all occurrences of consecutive commas with a single comma:
str.replace(/,+/g, ',');
You can use replace() method with regular expression with g flag to replace all instances ',,' with ',':
str.replace(/,,/g, ",");
Here's a simple example
var str = '0123456789,, 0987654213 ,, 0987334213';
str = str.replace(/,,/g, ",");
console.log(str);
var str = "0123456789,, 0987654213 ,, 0987334213,,"
str = str.split(",,").join(",")
console.log(str);
There is a replace method for String.
You can replace ',,' with a ','.
An example:
var str = "0123456789,, 0987654213,, 0987334213,";
var newStr = str.replace(/,,/g,','));
The output:
0123456789, 0987654213, 0987334213,
If I have a string like this 100,000 some digits with , followed by some other digits. I need to replace all digits so that the string become 100.
I have to use a 'replace()' function so please provide the expression.
var str = "1200,00";
str.replace("Expression");// Need this.
var str = "1200,00".split(",")[0];
alert(str);
using .replace it will replace all characters with '' after the comma.
var str = "1200,00".replace(/,.*/, '');
alert(str);
Use this Regular Expression:
var str = "1200,00";
str.replace(/,\d+/,'');
I tested and it works.
Hope that helps.
<script type="text/javascript">
var myValue = replace("1200,00");
function replace(str)
{
var retValue = str.replace(/,\d+/,'');
return retValue;
}
</script>
you can use a function for generic method
I have this string:
var str = "{view-map:{lonField_sad:!Longitude,latField:!Handicap_Accessible},currentView:!map}";
And I'm trying to replace the ALL the underscores with spaces for any word starting with exclamation sing (!) so the string should looks like this one:
var str = "{view-map:{lonField_sad:!Longitude,latField:!Handicap Accessible},currentView:!map}";
I spent a few hours trying to figure out how to do that without success.
Try to use function replacement form (example):
str.replace(/!\w+/g, function(x) { return x.replace(/_/g, ' '); })
Regexp /!\w+/g selects all words started with "!". After that we replace each word x with result of x.replace(/_/g, ' ').
You can use the regex
!([^_]+)_([^]+)
and replace with $1 $2
var str = "{view-map:{lonField_sad:!Longitude,latField:!Handicap_Accessible},currentView:!map}";
console.log(str.replace(/!([^_]+)_([^]+)/g, "!$1 $2"));
(![^_]+)_([a-zA-Z0-9]+)
Try this.See demo.
http://regex101.com/r/tF5fT5/57
var re = /(![^_]+)_([a-zA-Z0-9]+)/gm;
var str = '{view-map:{lonField_sad:!Longitude,latField:!Handicap_Accessible},currentView:!map}';
var subst = '$1 $2';
var result = str.replace(re, subst);