Length property not returning the desired value - javascript

I tried to get the length of this string:
$args[0]-match'\d.*?\/(.*)';$matches[1]
using:
console.log("$args[0]-match'\d.*?\/(.*)';$matches[1]".length);
I did this in the browser console. It returns 37. However, counting by hand, this string is 39 characters long. Am I missing something or is it a bug in the browser?

backslash character \ is a special escape character in strings so it doesn't count.
you can make backslashes count by preceding them with another backslash (that is escape the escape character):
console.log("$args[0]-match'\\d.*?\\/(.*)';$matches[1]".length)

\d is a char, no two.
you have to escape it
"$args[0]-match'\\d.*?\\/(.*)';$matches[1]".length

Related

why backslash(\) in a string is giving error in console

i have a string like
"C:\projects\cisco\iwan_staging_enc\enterprise-network-controller\ui-plugins\iwan"
when i paste into console and press enter, it is giving following error as
Uncaught SyntaxError: Invalid Unicode escape sequence
whats wrong here
Thanks
nageshwar
Since backslash is an escape character your string should be modified to:
"C:\\projects\\cisco\\iwan_staging_enc\\enterprise-network-controller\\ui-plugins\\iwan"
Please see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Escape_notation
The \u is the start of a unicode escape sequence, in your string you have a \u not followed by four hex numbers which is the format of unicode escape sequence \uxxxx. See
"C:\projects\cisco\iwan_staging_enc\enterprise-network-controller\u0050i-plugins\iwan"
\u0050 id P
Also there there are other types of escapes, so for instance if you had a \n somewhere in there you would get a newline
"C:\new projects\cisco\iwan_staging_enc\enterprise-network-controller\u0050i-plugins\iwan"
So if you do not want avoid these escape sequences escape the \s in the string with a slash before it.
"C:\\projects\\cisco\\iwan_staging_enc\\enterprise-network-controller\\ui-plugins\\iwan"

Getting an error with Console.log('\x') in JavaScript

the problem occurs only when the word after the '\' begin with 'x'. I would like to know if \x is reserved word.
It is not a reserved word, it's a way to print a character by its code:
>>> console.log('\x56');
V
In fact, Firebug tells you what's wrong if you omit the number:
SyntaxError: malformed hexadecimal character escape sequence
Like in many C-style languages, backslashes denote "escape sequences". More can be found here.
Backslash is used to escape characters. If you need a literal backslash, use \\.

regular expression incorrectly matching % and $

I have a regular expression in JavaScript to allow numeric and (,.+() -) character in phone field
my regex is [0-9-,.+() ]
It works for numeric as well as above six characters but it also allows characters like % and $ which are not in above list.
Even though you don't have to, I always make it a point to escape metacharacters (easier to read and less pain):
[0-9\-,\.+\(\) ]
But this won't work like you expect it to because it will only match one valid character while allowing other invalid ones in the string. I imagine you want to match the entire string with at least one valid character:
^[0-9\-,\.\+\(\) ]+$
Your original regex is not actually matching %. What it is doing is matching valid characters, but the problem is that it only matches one of them. So if you had the string 435%, it matches the 4, and so the regex reports that it has a match.
If you try to match it against just one invalid character, it won't match. So your original regex doesn't match the string %:
> /[0-9\-,\.\+\(\) ]/.test("%")
false
> /[0-9\-,\.\+\(\) ]/.test("44%5")
true
> "444%6".match(/[0-9\-,\.+\(\) ]/)
["4"] //notice that the 4 was matched.
Going back to the point about escaping, I find that it is easier to escape it rather than worrying about the different rules where specific metacharacters are valid in a character class. For example, - is only valid in the following cases:
When used in an actual character class with proper-order such as [a-z] (but not [z-a])
When used as the first or last character, or by itself, so [-a], [a-], or [-].
When used after a range like [0-9-,] or [a-d-j] (but keep in mind that [9-,] is invalid and [a-d-j] does not match the letters e through f).
For these reasons, I escape metacharacters to make it clear that I want to match the actual character itself and to remove ambiguities.
You just need to anchor your regex:
^[0-9-,.+() ]+$
In character class special char doesn't need to be escaped, except ] and -.
But, these char are not escaped when:
] is alone in the char class []]
- is at the begining [-abc] or at the end [abc-] of the char class or after the last end range [a-c-x]
Escape characters with special meaning in your RegExp. If you're not sure and it isn't an alphabet character, it usually doesn't hurt to escape it, too.
If the whole string must match, include the start ^ and end $ of the string in your RegExp, too.
/^[\d\-,\.\+\(\) ]*$/

Regex to allow special characters

I need a regex that will allow alphabets, hyphen (-), quote ('), dot (.), comma(,) and space. this is what i have now
^[A-Za-z\s\-]$
Thanks
I removed \s from your regex since you said space, and not white space. Feel free to put it back by replacing the space at the end with \s Otherwise pretty simple:
^[A-Za-z\-'., ]+$
It matches start of the string. Any character in the set 1 or more times, and end of the string. You don't have to escape . in a set, in case you were wondering.
You probably tried new RegExp("^[A-Za-z\s\-\.\'\"\,]$"). Yet, you have a string literal there, and the backslashes just escape the following characters - necessary only for the delimiting quote (and for backslashes).
"^[A-Za-z\s\-\.\'\"\,]$" === "^[A-Za-zs-.'\",]$" === '^[A-Za-zs-.\'",]$'
Yet, the range s-. is invalid. So you would need to escape the backslash to pass a string with a backslash in the RegExp constructor:
new RegExp("^[A-Za-z\\s\\-\\.\\'\\\"\\,]$")
Instead, regex literals are easier to read and write as you do not need to string-escape regex escape characters. Also, they are parsed only once during script "compilation" - nothing needs to be executed each time you the line is evaluated. The RegExp constructor only needs to be used if you want to build regexes dynamically. So use
/^[A-Za-z\s\-\.\'\"\,]$/
and it will work. Also, you don't need to escape any of these chars in a character class - so it's just
/^[A-Za-z\s\-.'",]$/
You are pretty close, try the following:
^[A-Za-z\s\-'.,]+$
Note that I assumed that you want to match strings that contain one or more of any of these characters, so I added + after the character class which mean "repeat the previous element one or more times".
Note that this will currently also allow tabs and line breaks in addition to spaces because \s will match any whitespace character. If you only want to allow spaces, change it to ^[A-Za-z \-'.,]+$ (just replaced \s with a space).

Strange javascript regular expressions

I have found the following regular expression
new RegExp("(^|\\s)hello(\\s|$)");
I refer http://www.javascriptkit.com/jsref/escapesequence.shtml for regular expressions..
But i cannot see \s escape sequence there..I know \s indicate whitespace character...
But what does the preceding \ do ..Which character is escaped?
I found similar regular expression in the Treewalker code in the following document http://ejohn.org/blog/getelementsbyclassname-speed-comparison/
The double \\ is to escape the backslash inside the string. In other word, \\ will be interpreted as \ for the regular expression.
The extra \ in this case is to escape the \ in the \s. Because we are inside a string declaration, you have to double up the \ to escape it. Once the string is processed and saved, it is reduced down to (^|\s)hello(\s|$)
The character immediately following the first \ is escaped. Normally \s escapes the s to mean "whitespace". In your example, the character which is escaped is \.
What you have is an expression which builds a regex (presumably to pass elsewhere) of (^|\s)hello(\s|$) — the word "hello" preceded either by whitespace or the start of the string, and followed by whitespace or the end of the string.
Essentially what the reg ex is doing, is looking for the opening and closing items of text surrounding the word hello and literally interpreting the '\s' as string content at the same time.
In laymans terms it's looking for a string that exactly matches:
|\shello\s|
As others have said the double \ is to escape the single \ so that instead of the reg ex engine looking for white-space it actually looks for '\s' as a string.
The ^ means start of line, the $ means end of line and the 2 | are interpreted as actual chars to look for
Lastly your start and end markers are bracketed () which means they will be extracted and placed in matches, which for you using C# means you can get at them by using:
myRegex.Matches.Group[1].Value
myRegex.Matches.Group[2].Value
1 being the beginning grouping, and 2 being the end.

Categories

Resources