-3

I am trying to make a regex that should exclude all digits and a string 'any'. I tried with /any|[0-9]/mig. that is to include. as I am a novice to regex, I don't know the proper way to exclude.

Chaitanya Chauhan
  • 743
  • 1
  • 11
  • 28

3 Answers3

3

You can use replace:

function remove(input) {
 return input.replace(/any|[0-9]+/ig,'');
}

console.log(remove('testANy2948234 me'));

Notice that /any|[0-9]+/ig matches "any" in "many". Of that isn't what you want, replace it with /\bany\b|[0-9]+/ig. \b matches a word break.

some
  • 48,070
  • 14
  • 77
  • 93
2

You can use /\d/ or [0-9]

Try this

var text="123haiany g Baby";
console.log(text.replace(/any|[\d]/mig, ''));

Please take a look for if you want : Should I use \d or [0-9] to match digits in a Perl regex?

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

So all you wanted is to select anything but digits or the word any. You didn't mention to replace any sort of that stuffs. So, if I have to give you a pure regex solution then it is possible to meet your criteria, but that will need negative lookbehind as well!!! Which I think java script only supports in google chrome.

You may try this:

[^any\d]|a(?!ny)|(?<=a)n(?!y)|(?<!a)n(?=y)|n(?!y)|(?<!an)y

Demo

A javascript compatible solution for the above regex would be:

[^n]y|[^a]ny|a(?!ny)|an(?!y)|[^a]n(?=y)|n(?!y)|[^any\d]

Javascript version Demo

const regex = /[^n]y|[^a]ny|a(?!ny)|an(?!y)|[^a]n(?=y)|n(?!y)|[^any\d]/g;
const str = `I successfully mande a regex to include it is any 0-9 but don't know the way to exclude. any zny znk anz ny any n a n y y ny ay zy any`;
let m;
var result="";
while((m = regex.exec(str)) !== null) {
        result+=m[0];
}
console.log(result);
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43