Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the number of characters without spaces?

Tags:

javascript

I'm new to this, so please understand me;/

I'm creating an app in appery.io and it has to count the number of letters of text inserted by the app user(without spaces).

I have an input field created(input), a button to press and show the result in a label(result)

the code for the button:

var myString = getElementById("input");

var length = myString.length;

Apperyio('result').text(length);

Can you please tell me what is wrong?

like image 526
user3096253 Avatar asked Oct 15 '14 18:10

user3096253


1 Answers

To ignore a literal space, you can use regex with a space:

// get the string
let myString = getElementById("input").value;

// use / /g to remove all spaces from the string
let remText = myString.replace(/ /g, "");

// get the length of the string after removal
let length = remText.length;

To ignore all white space(new lines, spaces, tabs) use the \s quantifier:

// get the string
let myString = getElementById("input").value;

// use the \s quantifier to remove all white space
let remText = myString.replace(/\s/g, "")

// get the length of the string after removal
let length = remText.length;
like image 156
SReject Avatar answered Nov 09 '22 22:11

SReject