Groups | Blog | Home
all groups > asp.net > february 2008 >

asp.net : Dumb JavaScript question - how to use \\ not as a comment?



John Kotuby
2/10/2008 9:45:52 PM
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.

Scott M.
2/10/2008 9:55:59 PM
Double forward-slashes (//) are comments in JavaScript. Double back-slashes
(\\) are an escape code for a single back-slash.

-Scott


[quoted text, click to view]

John Kotuby
2/10/2008 10:04:35 PM
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.


[quoted text, click to view]
AddThis Social Bookmark Button