1

I need a javascript regex object that brings back any matches of symbols in a string, take for example the following string:

input =    !"£$[]{}%^&*:@\~#';/.,<>\|¬`

then the following code:

input.match(regExObj,"g");

would return an array of matches:

[[,!,",£,$,%,^,&,*,:,@,~,#,',;,/,.,,,<,>,\,|,¬,`,]]

I have tried the following with no luck.

match(/[U+0021-U+0027]/g);

and I cannot use the following because I need to allow none ascii chars, for example Chinese characters.

[^0-9a-zA-Z\s]
Farhad-Taran
  • 6,282
  • 15
  • 67
  • 121
  • 1
    What do you have so far and where are you getting stuck? – Jasper Nov 08 '12 at 16:26
  • 1
    u can use this `[^0-9a-zA-Z\s]` – Anirudha Nov 08 '12 at 16:27
  • @Fake.It.Til.U.Make.It I cant use [^0-9a-zA-Z\s] because the app allows for entry of noneascci chars and that would limit the input to only english alphabets. – Farhad-Taran Nov 08 '12 at 16:28
  • @xerxes do you want to match symbols belonging to only ascii – Anirudha Nov 08 '12 at 16:31
  • possible duplicate of [How can I match multiple occurrences with a regex in JavaScript similar to PHP's preg\_match\_all()?](http://stackoverflow.com/questions/520611/how-can-i-match-multiple-occurrences-with-a-regex-in-javascript-similar-to-phps) – Phil H Nov 08 '12 at 16:31
  • I wanna match any of these symbols> !"£$[]{}%^&*:@\~#';/.,<>\|¬` – Farhad-Taran Nov 08 '12 at 16:31

2 Answers2

2
var re = /[!"\[\]{}%^&*:@~#';/.<>\\|`]/g;
var matches = [];
var someString = "aejih!\"£$[]{}%^&*:@\~#';/.,<>\\|¬`oejtoj%";
while(match = re.exec(someString)) {
    matches.push(match[1]);
}

Getting

['!','"','[',']','{','}','%','^','&','*',':','@','~','#',''',';','/','.','<','>','\','|','`','%]
Phil H
  • 19,928
  • 7
  • 68
  • 105
1

What about

/[!"£$\[\]{}%^&*:@\\~#';\/.,<>|¬`]/g

?

sp00m
  • 47,968
  • 31
  • 142
  • 252