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.
Asked
Active
Viewed 352 times
-3

Chaitanya Chauhan
- 743
- 1
- 11
- 28
-
1any is a string just like 'anubhava' or 'chaitanya' or 'any' – Chaitanya Chauhan Jan 22 '18 at 06:39
3 Answers
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
-
@guest271314 That's what i need test it before post the answer :( Sorry for it – Ramesh Rajendran Jan 22 '18 at 06:51
-
-
@some , I want [0-9] and 'any' should not be included without using any javascript methods – Chaitanya Chauhan Jan 22 '18 at 07:14
-
2@ChaitanyaChauhan: If you don't provide all the necessary information in question, you will get answers based on our interpretation only. – anubhava Jan 22 '18 at 07:17
-
-
my bad, I didn't add that I want it without any javascript function. please check the improvised question. – Chaitanya Chauhan Jan 22 '18 at 07:24
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
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]
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
-
thanks a lot, rizwan! Please let me know the documentation from where I can learn it. – Chaitanya Chauhan Jan 22 '18 at 07:21
-