What is the usage for \w? - javascript

I have a string: string-a
I'm trying to match this string with the following regex: string-\w{1,2}. I'd like to match anything in the form of "string-aa" where 'aa' can be one or two of any letter or digit.
But running 'string-a'.match('string-\w{1,2}'); returns null in the console. Why does that happen? If I run this in a debugging tool like Debuggex, it works perfectly fine.
I'm not sure I understand how \w is used. Any help would be appreciated!

You are matching as a text not regular expression. you should change it to:
'string-a'.match(/string-(\w{1,2})/);

For 'string-a'.match('string-\w{1,2}'); to work in the console you need to escape the \.
So try,
'string-a'.match('string-\\w{1,2}'); and it will work.
The \w is used as you may expect, it matches a word character.

Related

java script string.match() doesn't work as expected

I am trying to match a string with regex as below. Jsfiddle returns the array with matched string as expected. But if I run the below statement in browser console, it returns null. I tried in ie11,Chrome,Mozilla. Can anybody explain why this discrepancy? Am I missing something.
"201458".match(/^20['^\s']{4,}$|^$/)
It should not match. You are looking for 20 followed by four or more of apostrophe, caret or a whitespace character (followed by a string end); 1 is none of those.
['^] is "apostrophe or caret".
[^'] is "not apostrophe".
Caret only has its special function when at start of the character class; apostrophe does not have any special function in a regexp.
If you find that this matches in jsfiddle, please link the said fiddle; I will be very surprised.

Exact string negation in javascript regexpressions

This is more a question to satisfy my curiosity than a real need for help, but I will appreciate your help equally as it is driving me nuts.
I am trying to negate an exact string using Javascript regular expressions, the idea is to exclude URL that include the string "www". For instance this list:
http://www.example.org/
http://status.example.org/index.php?datacenter=1
https://status.example.org/index.php?datacenter=2
https://www.example.org/Insights
http://www.example.org/Careers/Job_Opportunities
http://www.example.org/Insights/Press-Releases
For that I can succesfully use the following regex:
/^http(|s):..[^w]/g
This works correctly, but while I can do a positive match I cannot do something like:
/[^www]/g or /[^http]/g
To exclude lines that include the exact string www or http. I have tried the infamous "negative Lookeahead" like that:
/*(?: (?!www).*)/g
But this doesn't work either OR I cannot test it online, it doesn't works in Notepad++ either.
If I were using Perl, Grep, Awk or Textwrangler I would have simply done:
!www OR !http
And this would have done the job.
So, my question is obviously: What would be the correct way to do such thing in Javascript? Does this depend on the regex parser (as I seem to understand?).
Thanks for any answer ;)
You need to add a negative lookahead at the start.
^(?!.*\bwww\.)https?:\/\/.*
DEMO
(?!.*\bwww\.) Negative lookahead asserts that the string we are going to match won't contain, www.. \b means word boundary which matches between a word character and a non-word character. Without \b, www. in your regex would match www. in foowww.
To negate 'www' at every position in the input string:
var a = [
'http://www.example.org/',
'http://status.example.org/index.php?datacenter=1',
'https://status.example.org/index.php?datacenter=2',
'https://www.example.org/Insights',
'http://www.example.org/Careers/Job_Opportunities',
'http://www.example.org/Insights/Press-Releases'
];
a.filter(function(x){ return /^((?!www).)*$/.test(x); });
So at every position check that 'www' doesn't match, and then match
any character (.).

Javascript RegEx - invalid quantifier

I saw the other posts but none of them help me ...
So, i tried to match url in a string in javascript with regex it works perfectly on regex101 but fails in javascript.
var matches = feed.content.match(
'/((http|https|ftp):\/\/([a-zA-Z0-9\.\-\_\%]+\/?){1}([a-zA-Z0-9\.\-\_]+\/?)*(\?[a-zA-Z0-9\.\-\_\%\+\=\&\:]*)*)/ig'
);
And firebug returns me
SyntaxError: invalid quantifier
Please can you help me ?
As pointed out in the comments, you should remove the single quotes enclosing the regex. As well as that, I would propose making a few changes to the expression itself:
((https?|ftp):\/\/([\w.%-]+\/?)([\w.-]+\/?)*(\?[\w.%+=&:-]*)*)
The ? after the smeans that it is optional, so http and https will both match. \w is the word character class, so that covers A-Za-z0-9_ much more concisely. There's no need to escape all the symbols but a useful trick is to put the - at the end of the character class, so that it isn't interpreted as a range between two characters. The {1} isn't necessary as that's the default behaviour.
updated on regex101
You're passing the regex as a string - just get rid of the outer quotes.
var matches = feed.content.match(
/((http|https|ftp):\/\/([a-zA-Z0-9\.\-\_\%]+\/?){1}([a-zA-Z0-9\.\-\_]+\/?)*(\?[a-zA-Z0-9\.\-\_\%\+\=\&\:]*)*)/ig
);

Two-digit RegEx Patterm Match in JavaScript

The following expression:
targetString = targetString.replace(parenthesizedRegEx, "$3$1$11");
where parenthesizedRegEx is a valid parenthesized regular expression, replaces the matched text with a string that is the concatenation of the third item, the first item, the first item again, and the literal "1". It is as if it is ignoring the "two-digit" parentheses-item index "$11" and treating it as "$1" and the literal "1".
Is there some escaping or other separating that should be used?
This result occurs in FF and IE9.
Thanks for your help. I hope the answer is embarrassingly simple!!
Edit Update:
I did a jfiddle to demonstrate the issue comprehensively. The regexp I am using includes a negative lookahead assertion. It seems that when I include all the open parens for the assertion, it fails. If I include none of the insertion's open parens, it also fails. But if I include all but the assertion's initial opening paren, it works. I know that groups formed with (?:...) are not numbered. But is seems that one has to include all the other open parens within the assertion to get the count right. So you will see in the jfiddle that $11 does not work but that $10 does.
http://jsfiddle.net/pxMFx/1/
Thanks for looking at this.
This works fine for me:
var regex = /^(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w).*$/i;
alert("abcdefghijklmno".replace(regex,"$3$1$11"));
http://jsfiddle.net/J3RAa/
The key is that you need to match the whole string with your regular expression if you are targeting a specific location in it. Try taking the .* out of the above regex and you will see that it breaks the replacement.

Find consecutive "//" in regex in JavaScript

I gave it a college try, but I'm stumped. I'm trying to find consecutive slashes within a string. The rest of the regex works great, but the last part I can't quite get.
Here's what I have:
val.match( /^[\/]|[~"#%&*:<>?\\{|}]|[\/|.]$/ )
and finding this thread, I decided to update my code to no avail:
RegEx to find two or more consecutive chars
val.match( /^[\/]|[\/]{2,}|[~"#%&*:<>?\\{|}]|[\/|.]$/ )
What do I need to get this thing going?
So, I need this regex to look for many characters. That would explain the first code sample that I provided:
val.match( /^[\/]|[~"#%&*:<>?\\{|}]|[\/|.]$/ )
What I need it to also do, is look in the string for a double whack. Yes, I'm well aware of indexOf and other string manipulation techniques, but I labeled it regex because it needs to be. Let me know if you need more info...
Uh, why aren't you just doing
/\/{2,}/g
? Your regexes in the OP seem way more complicated...
\/ matches a literal backslash character
{2,} tells to match it twice or more
/g makes the pattern global so you can find all occurences of the pattern in your strings.
[\/]+ should match one or more /s.
/(.)$1+/
would find any place where a single character occurs 2 or more times. the (.) matches a single character, and captures that character into $1, which you then require to be immediately after the initial character, 1 or more times.
For slashes, you can simplify it down to
/\/{2,}/
/\/\/+/
but then you're into leaning toothpick territory.
Why not use indexof? That would be simpler.
Here's the answer.
val.match( /^[\/|_]|[~"#%&*:<>?\\{|}]|[\/]{2,}|[\/|.]$/ )
Not sure why the other version doesn't work, but maybe someone could shed some light onto the matter.
Tests:
_text - Failed leading underscore
/text - Falied leading whack
text~moreText - Failed contains invalid character: ~"#%&*:<>?\{|}
text//text - Failed double whack
text/ - Failed trailing whack
text. - Failed trailing period
Not sure why the code below wasn't working, but moved the double whack test and it works now:
val.match( /^[\/|_]|[\/]{2,}|[~"#%&*:<>?\\{|}]|[\/|.]$/ )

Categories