2

I'm having difficulty scanning for the special character "[". After looking around, I've tried:

var regEx = new RegExp(someValue + "[\x5B]");  // "5B" is the hex-value for "["

(I DO want case-sensitivity and I ONLY want the first occurrence so I intentionally didn't add any modifiers.)

I have been looking and looking, and I still have no idea what I'm doing wrong. If this is a bone-headed question, I'm sorry, but I'm new to JavaScript, and I'm still learning all the ins and outs.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Zak
  • 12,213
  • 21
  • 59
  • 105
  • 2
    It works for me: `RegExp("[\x5B]").test("[")` yields true. – Gumbo Apr 10 '11 at 15:49
  • Thank you everyone for the quick answers, I truly appreciate it. However, to whom ever cast the down vote, thanks for having patience with a newbie; I'm only trying to learn. – Zak Apr 10 '11 at 16:11
  • 1
    Regexes are _fun!_ But I would strongly recommend spending an hour or two studying the basics. For starters, you need to learn which characters are special: "metacharacters" that need to be escaped (and the rules are different inside and outside character classes.) There is an excellent online tutorial at: [www.regular-expressions.info](http://www.regular-expressions.info/). The time you spend there will pay for itself many times over. Happy regexing! – ridgerunner Apr 10 '11 at 16:49
  • I've saved it in my bookmarks! Thanks for the advice, I bumped up your comment. Cheers! – Zak Apr 10 '11 at 16:57

5 Answers5

4
function escapeRegExp(str) {
  return str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

var re = new RegExp(escapeRegExp(str));   

See: Escape string for use in Javascript regex

Community
  • 1
  • 1
coolaj86
  • 74,004
  • 20
  • 105
  • 125
1

Should be fine, if you just escape the character:

new RegExp(someValue + "\\[");

You need two backslashes because the backslash is the escape character for strings too. So \\[ will result in the string \[ which is what we need.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thank you, this is a perfect explanation. I selected the other answer because it popped up first, but I gave you an up vote for the amazing help! Thanks, – Zak Apr 10 '11 at 16:40
1
var regEx = new RegExp(someValue + "\\[");

You simply have to escape it with a backslash. The backslash is double because it is a javascript special character as well.

Håvard
  • 9,900
  • 1
  • 41
  • 46
  • You're right, this is perfect! I was trying to use RegExp.lastIndex(), which does not work without the "g" modifier. It was, in essence, turned off, and I was trying to use it to tell me if my search had a result. Thank you, – Zak Apr 10 '11 at 16:42
0

Both the regex .\[ and .[\x5B] match the sub-string "e[" from the text:

"some[text"

as you can see yourself:


http://ideone.com/iR78l

var text = "some[text";
print(text.match(/.\[/));

and http://ideone.com/qvsCz

var text = "some[text";
print(text.match(/.[\x5B]/));
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
-2

I think all you need is /[/ for your expression to find the first occurrence of "["

If there's something more complicated you're looking for, check out this tool: http://www.gskinner.com/RegExr/

It's a life-saver for creating regular expressions.

Mike
  • 1
  • 1