Check if string has more than one character javascript

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 length 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.

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

  • We have 3 elements in the HTML file [div, button, and h2]. The div element is just a wrapper for the rest of the elements.
  • The innerText for the button element is “Check” and for the h2 element, it is “Result”.
  • We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.
  • We have also included our javascript file script.js with a script tag at the bottom.



  
  
  
  
  Document




  
Check

Result

.container {        
    text-align: center;
}

button {
  margin-top: 10px;
  padding: 10px 20px;
}

Javascript

  • We have selected the button element and h2 element using the document.querySelector[] method and stored them in btnCheck and output variables respectively.
  • We have attached a click event listener to the button element.
  • We have a global variable myString which holds a string as its value.
  • In the event handler function, we are using the 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.
  • Depending upon the result of the check, we will assign “Yes” or “No” to the result variable.
  • We are displaying the 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;
  
}];

How do you check whether a string contains all alphabets in JavaScript?

JavaScript: HTML Form validation - checking for all letters.
Javascript function to check for all letters in a field function allLetter[inputtxt] { var letters = /^[A-Za-z]+$/; if[inputtxt.value.match[letters]] { return true; } else { alert["message"]; return false; } } ... .
Flowchart:.
HTML Code

How do you check if a string contains only one character?

If the task is "check if all the symbols in a string are the same", s == len[s] * s[0] is a valid answer [no symbols mean an error, and exception is ok].

How do you check if there is more than one character in a string?

You can compare IndexOf to LastIndexOf to check if there is more than one specific character in a string without explicit counting: var s = "12121.23.

How do you check if a string contains multiple characters in JS?

The includes[] method is used to perform a case-sensitive search to detect whether a string contains another string or not and returns a Boolean value. The every[] method executes a certain function for each element in an array.

Chủ Đề