remove second comma from a string in javascript - javascript

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,

Related

How to replace a string with square brackets using javascript replace function?

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

Replace expression between two punctuation letters in javascript

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

Replace string with substring with regular expression

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

How I can replace a underscore with an space inside a word starting with an special char in javascript?

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

Replace comma by double quote in javascript

How to replace comma by double quote in javascript?
For example: "c,C#,JavaScript" should be like "C","C#","JavaScript"
str = '"c,C#,JavaScript"';
str = str.split(',').join('","');
That would result in "c","C#","JavaScript"
var original = '"c,C#,JavaScript"';
var quoted = original.replace(/,/g, '","'); // "c","C#","JavaScript"
Just to toss it in there, you could also .split() and .join(), like this:
var oldString = '"c,C#,JavaScript"';
var newString = oldString.split(',').join('","');
You can test it here.
You can do this:
str = "c,C#,JavaScript";
str = str.replace(/,/g, '"');
Result:
c"C#"JavaScript

Categories

Resources