How to remove numbers from a string?

前端 未结 7 1672
遇见更好的自我
遇见更好的自我 2020-12-12 19:56

I want to remove numbers from a string:

questionText = \"1 ding ?\"

I want to replace the number 1 number and the question mar

相关标签:
7条回答
  • 2020-12-12 20:36

    You're remarkably close.

    Here's the code you wrote in the question:

    questionText.replace(/[0-9]/g, '');
    

    The code you've written does indeed look at the questionText variable, and produce output which is the original string, but with the digits replaced with empty string.

    However, it doesn't assign it automatically back to the original variable. You need to specify what to assign it to:

    questionText = questionText.replace(/[0-9]/g, '');
    
    0 讨论(0)
  • 2020-12-12 20:38

    You can use .match && join() methods. .match() returns an array and .join() makes a string

    function digitsBeGone(str){
      return str.match(/\D/g).join('')
    }
    
    0 讨论(0)
  • 2020-12-12 20:42

    This can be done without regex which is more efficient:

    var questionText = "1 ding ?"
    var index = 0;
    var num = "";
    do
    {
        num += questionText[index];
    } while (questionText[++index] >= "0" && questionText[index] <= "9");
    questionText = questionText.substring(num.length);
    

    And as a bonus, it also stores the number, which may be useful to some people.

    0 讨论(0)
  • 2020-12-12 20:43

    A secondary option would be to match and return non-digits with some expression similar to,

    /\D+/g
    

    which would likely work for that specific string in the question (1 ding ?).

    Demo

    Test

    function non_digit_string(str) {
    	const regex = /\D+/g;
    	let m;
    
    	non_digit_arr = [];
    	while ((m = regex.exec(str)) !== null) {
    		// This is necessary to avoid infinite loops with zero-width matches
    		if (m.index === regex.lastIndex) {
    			regex.lastIndex++;
    		}
    
    
    		m.forEach((match, groupIndex) => {
    			if (match.trim() != '') {
    				non_digit_arr.push(match.trim());
    			}
    		});
    	}
    	return non_digit_arr;
    }
    
    const str = `1 ding ? 124
    12 ding ?
    123 ding ? 123`;
    console.log(non_digit_string(str));


    If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


    RegEx Circuit

    jex.im visualizes regular expressions:

    0 讨论(0)
  • 2020-12-12 20:51

    Very close, try:

    questionText = questionText.replace(/[0-9]/g, '');
    

    replace doesn't work on the existing string, it returns a new one. If you want to use it, you need to keep it!
    Similarly, you can use a new variable:

    var withNoDigits = questionText.replace(/[0-9]/g, '');
    

    One last trick to remove whole blocks of digits at once, but that one may go too far:

    questionText = questionText.replace(/\d+/g, '');
    
    0 讨论(0)
  • 2020-12-12 20:51

    String are immutable, that's why questionText.replace(/[0-9]/g, ''); on it's own does work, but it doesn't change the questionText-string. You'll have to assign the result of the replacement to another String-variable or to questionText itself again.

    var cleanedQuestionText = questionText.replace(/[0-9]/g, '');
    

    or in 1 go (using \d+, see Kobi's answer):

     questionText = ("1 ding ?").replace(/\d+/g,'');
    

    and if you want to trim the leading (and trailing) space(s) while you're at it:

     questionText = ("1 ding ?").replace(/\d+|^\s+|\s+$/g,'');
    
    0 讨论(0)
提交回复
热议问题