3

Aside from Javascript, all instances of regular expressions use something like (for finding a number in brackets) "\\[[0-9]+\\]" or r"\[[0-9]+\]". That string is then used in a function like Contains("\\[[0-9]+\\]", "[1009] is a number."). Regex strings in Javascripts are not encapsulated at all, so I see things like var patt = /w3schools/i. Why is this? How does Javascript tell the difference between this and other content? Why not just use normal strings?

2
  • no other content begins and ends with a singular /. It's just syntactic sugar for new RegExp(). Commented Jul 18, 2015 at 14:00
  • 1
    Having regular expression literlas is actually a benefit of the language. Ever tried writing one for Java? You quickly end up in escaping hell. Commented Jul 18, 2015 at 14:18

3 Answers 3

3

Why is this?

That's just how regex literals work. Regular expressions are objects in JS, not plain strings.

How does Javascript tell the difference between this and other content?

That's just how the language grammar is defined. In fact it makes it much easier to tell the difference between a string and a regex than in other languages.

Why not just use normal strings?

Because escaping works different. Other languages use "raw" strings for this, which JavaScript doesn't (didn't) have. Instead, they introduced a literal notation for regular expressions - using / as a delimiter (borrowed from Perl).

Of course, you still can use normal strings, and create a regex object using the RegExp constructor, but for static expressions the literal syntax is much simpler.

Sign up to request clarification or add additional context in comments.

Comments

2

Well, they are not strings to begin with. The are regex literals.

How does Javascript tell the difference between this and other content?

Just like the " are used to delimit string literals, or [...] are used to delimit array literals, / are used to delimit regular expression literals.

Why not just use normal strings?

Regular expression have different special characters and different escaping rules. That's why you have to use double escapes if you use a string with RegExp (e.g. "\\[[0-9]+\\]"). Many people get that wrong and it's a bit confusing.

So it makes sense to have a representation of regular expression that is not "inside" of another abstraction (strings).

Comments

0

Regular expressions in JavaScript are objects not strings.

var regex = /[0-9]/;
console.log(typeof regex); // "objec"

Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec and test methods of RegExp, and with the match, replace, search, and split methods of String. This chapter describes JavaScript regular expressions.

Regular Expressions

The opening and closing / are not part of the expression they are just marking a regex literal just like {} is marking an object literal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.