I have a string that might have multiple commas in a row. I want to find every time it has more than one comma, I want it to be replaced with only one comma. How can I do this?
Thanks
asked Dec 30, 2013 at 23:11
0
Use a regular expression:
To test if a string contains multiple commas in a row:
var result = /,,/.test[input];
To replace them with just one:
var result = input.replace[/,+/g, ','];
answered Dec 30, 2013 at 23:12
p.s.w.gp.s.w.g
143k29 gold badges282 silver badges320 bronze badges
To replace two or more consecutive commas with a single comma, you can use this:
str = str.replace[/,{2,}/g, ","];
The {2,}
after the comma means two or more of whatever the preceeding character was in the regex.
The g
flag tells it to replace all occurrences of that in the string.
Working demo: //jsfiddle.net/jfriend00/pxhLH/
answered Dec 30, 2013 at 23:14
jfriend00jfriend00
651k90 gold badges925 silver badges926 bronze badges
Use a simple loop and replace. Plug this into your page to see it work.
var str = ",,, I have ,, some extra ,, commas ,,";
while [str.indexOf[",,"] > -1] {
str = str.replace[",,", ","]
}
alert[str];
The only part you need is the while loop. If you want you can make it into a function where str is the parameter and then kick out your new string.
answered Dec 30, 2013 at 23:34
DeadlyChambersDeadlyChambers
5,0794 gold badges43 silver badges57 bronze badges
In this tutorial, you will learn how to check if string has more than one character in javascript. A string consists of multiple characters and these characters can be letters, numbers, or special characters. For a beginner, it can be a bit tricky to find if a string has more than one character. There are numerous ways to check if a string has more than one character. But for the sake of simplicity, we will use In the following example, we have one global variable that holds a string. Upon click of a button, we will check if the string has more than one character and display the result on the screen. Please have a look over the code example and
the steps given below. HTML & CSS Javascriptlength
property and ternary
operator [?
]. The length
property returns the length of the string. If the string is empty, then it will return 0. The ternary operator is also known as the conditional operator which acts similar to the if-else statement.div
, button
, and h2
]. The div
element is just a wrapper for the rest of the elements.innerText
for the button
element is “Check”
and for the h2
element, it is “Result”
.style.css
stylesheet inside the head
element.script.js
with a script
tag at the bottom.
Document
Result
.container {
text-align: center;
}
button {
margin-top: 10px;
padding: 10px 20px;
}
button
element and h2
element using the document.querySelector[]
method and stored them in btnCheck
and output
variables respectively.click
event listener to the button
element.myString
which holds a string as its value.length
property and ternary operator [?
]
to check if myString
length greater than 1. If the length is greater than 1, that means the string has more than one character.“Yes”
or “No”
to the result
variable.result
in the h2
element using the innerText
property.let btnCheck = document.querySelector["button"];
let output = document.querySelector["h2"];
let myString = "Mary is new to this city.";
btnCheck.addEventListener["click", [] => {
let result = myString.length > 1 ? "Yes" : "No";
output.innerText = result;
}];