Dumb JavaScript question - how to use \\ not as a comment?

J

John Kotuby

Hi all,

I am trying to learn how to use regular expression objects in JavaScript.
I am missing some very basic JavaScript concept.
While playing around with regular expressions from a 3rd party regex
creator, I tried pasting a regex expression that contained \\ into a
JavaScript function which, of course, interpreted the double backslash as a
comment indicator.

The JScript documentation states:
"Notice that because the backslash itself is used as the escape character,
you cannot directly type one in your script. If you want to write a
backslash, you must type two of them together (\\)."

Also I have seen that to declare a regular expression object it is not
correct to use apostrophes or double quotes to delimit the expression.
For example...

theRegExpObj=/(^\s*)|(\s*$)/g

rather than

theRegExpObj="/(^\s*)|(\s*$)/g"

So back to my original question. How does one use \\ in a JavaScript
function if it is meant as an escape code for the backslash rather than the
beginning of a comment?

Thanks for any answers.
 
S

Scott M.

Double forward-slashes (//) are comments in JavaScript. Double back-slashes
(\\) are an escape code for a single back-slash.

-Scott
 
J

John Kotuby

Right Scott...

Dumb question it was indeed ;-)

Actually the problem I ran into did involve // found in a regular expression
that was generated by a regular expression builder.

Perhaps the expression generated was a mistake by the 3rd party program, but
I know I tried to paste it into a JavaScript function and it wouldn't work.

I will take more time to study regex and see if there is any case where I
would actually come across the occasion to use // in a regular expression. I
suspect there might be, possibly when doing a search match on a forward
slash.

I hope everyone got a good laugh over my mistake. I know I did.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top