1

I want to remove all numeric values from a string But only if the string contains at least one letter.

How can I do this in JavaScript?

For e.g.

var s = "asd23asd"

Then result must be asdasd

However if

var s = "123123"

Then result must be 123123 as the string does not have any letters.

8
  • 6
    What did you tried? Commented Jun 5, 2015 at 9:55
  • I tried using a condition. I just wanted to know if there was a direct way of doing it Commented Jun 5, 2015 at 10:06
  • @Roberto i mean the string should contain atleast one character which is some alphabet from a-z Commented Jun 5, 2015 at 10:07
  • So -1 should remain -1, or +2 remain +2. What about a+2 should it be a or a+ and what about whitespace or characters from other alphabets like å, ä or ö, or characters _!:;, and so on? Commented Jun 5, 2015 at 10:17
  • @Xotic750 my string does not have any special characters other than '-' or '_'. I had already taken care of that case Commented Jun 5, 2015 at 10:30

4 Answers 4

13
function filter(string){
     var result = string.replace(/\d/g,'')
     return result || string;
}

or directly

var newString = string.replace(/\d/g,'') || string;

Why || works

the || and & are conditionals operators and sure that you used in if, while ...

If you do somethin like

var c1 = false, c2 = true, c3= false, c4 = true;
if( c1 || c2 || c3 || c4) {
}

This evaluation will stop in the first moment that is valid or invalid.

this mind that the evaluation stop in c2 this mind that is more fast (true || false) than (false || true)

At this point we can add another concept, the operator return always the last element in the evaluation

(false || 'hey' || true) return 'hey', remember in JS 'hey' is true but '' is false

Interesting examples:

var example = {
  'value' : {
     'sub_value' : 4
  }
}

var test = example && example.value && example.value.sub_value;
console.log(test) //4

var test_2 = example && example.no_exist && example.no_exist.sub_value;
console.log(test_2) //undefined


var test_3 = example.valno_existue.sub_value; //exception

function test_function(value){
   value = value || 4; //you can expecify default values
}
Sign up to request clarification or add additional context in comments.

5 Comments

is g and gi is same?what is the use of gi? @Raúl Martín
@Raúl Martín fine thanks, before using || string, we are getting the result na,then may i know how it works further?
I updated, maybe you can edit the question and help me with my English ;)
@Raúl Martín extremely sorry :( i could n't understand ji.... and i think no one help for his second requirement
3

You can try this. First check if the word contains any alphabet, if yes then replace.

var s = "asd23asd";
if(/\w+/.test(s))
    s = s.replace(/\d+/g, '');

Comments

0
([a-zA-Z]+)\d+|\d+(?=[a-zA-Z]+)

You can try this.Replace by $1.See demo.

https://regex101.com/r/nS2lT4/27

Comments

-1

Javascript Code

  var txt='asd23ASd3';
  if(parseInt(txt))
      var parsed=txt;
  else
      var parsed=txt.replace ( /[^a-zA-Z]/g, '');
  console.log(parsed)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.