简体   繁体   中英

Removing numbers and characters from a string

If a user clicks on a tag, right now it recognizes the full text (numbers and characters).

returns Popular Tag %82

I've tried:

$tag.click(function(){
  var $this = $(this);
  var text = $this.text();
  var num = text.parseInt();
  num.remove();
  alert(text)
});

Doesn't do the trick for numbers. So how would I get just the letters? Ie: ignoring BOTH numbers and special characters(%)

Fiddle here! Thanks for you help!

The simplest way is to use a regex:

var s = "ABCblahFoo$%^3l";
var letters = s.replace(/[^A-Za-z]+/g, '');
  • the [] represents a character (or a number of different possible characters)
  • the ^ means all characters EXCEPT the ones defined in the brackets
  • AZ equals capital letters
  • az equals lowercase letters

So... Replace every character that is NOT a letter with the empty string.

jsFiddle Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM