2

I'm trying to find a JavaScript regexp for this string: ![](). It needs to be an exact match, though, so:

`!()[]`      // No match
hello!()[]   // No match
!()[]hello   // No Match
!()[]        // Match
 !()[]       // Match (with a whitespace before and/or after)

I tried this: \b![]()\b. It works for words, like \bhello\b, but not for those characters.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jumbalaya Wanton
  • 1,601
  • 1
  • 25
  • 47

4 Answers4

3

The characters specified are control characters and need to be escaped also user \s if you want to match whitespace. Try the following

\s?!(?:\[\]\(\)|\(\)\[\])\s?

EDIT: Added a capture group to extract ![]() if needed

EDIT2: I missed that you wanted order independant for [] and () I've added it in this fiddle http://jsfiddle.net/MfFAd/3/

Hugo Tunius
  • 2,869
  • 24
  • 32
  • This has a small issue. Your second edit worked perfectly, but it did not account for `!`; your last edit, though, creates two elements and a white space before the element. – Jumbalaya Wanton Dec 18 '13 at 21:59
  • OK, last edit almost works, but leaves a preceding space. Console output: `" ![]()"` – Jumbalaya Wanton Dec 18 '13 at 22:01
  • I depreped and posted the wrong link. Anyways to remove the space you'll have to add a capture group around `!(?:\[\]\(\)|\(\)\[\])` and then use `out[1]` to get the captured result – Hugo Tunius Dec 18 '13 at 22:02
  • Also, you were right earlier. They are order dependent. It was a typo. It always has to be `![]()`. There's never any `!()[]` – Jumbalaya Wanton Dec 18 '13 at 22:04
  • 1
    Ah okay well I'll leave the answer like this in any case, I trust you can change it to fit your needs. – Hugo Tunius Dec 18 '13 at 22:06
1

This matches your example:

\s*!\[\]\(\)\s*

Regular expression visualization

Though the match also includes the spaces before and after !()[].

I think \b does not work here because ![]() is not a word. Check out this quote from MDN:

\b - Matches a word boundary. A word boundary matches the position where a word character is not followed or preceeded by another word-character. Note that a matched word boundary is not included in the match. In other words, the length of a matched word boundary is zero.

rzymek
  • 9,064
  • 2
  • 45
  • 59
  • Sorry, The order dependence was a typo on my part. Round brackets are ALWAYS before square brackets. – Jumbalaya Wanton Dec 18 '13 at 22:05
  • THat simplifies it a lot. Edited. – rzymek Dec 18 '13 at 22:08
  • The only issue i have with this is that it captures the spaces around the string. I want the console output to be `["![]()"]` and not `[" ![]()"]`. Fiddle: http://jsfiddle.net/MfFAd/4/ – Jumbalaya Wanton Dec 18 '13 at 22:11
  • You could capture spaces `(\s)` and [include them](http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex) in replace or whatever you're doing – rzymek Dec 18 '13 at 22:22
1

Let's create a function for convenience :

function find(r, s) {
    return (s.match(r) || []).slice(-1);
}

The following regular expression accepts only the searched string and whitespaces :

var r = /^\s*(!\[\]\(\))\s*$/;
find(r, '![]() ');      // ["![]()"]
find(r, '!()[] ');      // []
find(r, 'hello ![]()'); // []

This one searches a sub-string surrounded by whitespaces or string boundaries :

var r = /(?:^|\s)(!\[\]\(\))(?:\s|$)/;
find(r, '![]() ');      // ["![]()"]
find(r, 'hello ![]()'); // ["![]()"]
find(r, 'hello![]()');  // []
0

To match all characters except letters and numbers you can use this regex

/[^A-Z0-9]/gi

g - search global [ mean whole text, not just first match ] i -case insensitive

to remove any other sign for example . and ,

/[^A-Z0-9\.\,]/gi

In order to match exact string you need to group it and global parameter

/(\!\[\]\(\))/g 

so it will search for all matches

Senad Meškin
  • 13,597
  • 4
  • 37
  • 55