Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

javascript - How to count the number of characters without spaces?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...