How to count the number of characters without spaces?

前端 未结 3 1121
渐次进展
渐次进展 2020-12-06 14:23

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 spac

3条回答
  •  -上瘾入骨i
    2020-12-06 14:35

    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;
    

提交回复
热议问题