Can anyone help getting a piece of regex working in javascript please?
This is the regular expression:
storify.com\/*(?<code>[^"]*)
It works fine in C# but I can't get it working in js, I presume due to a delimiter issue but not sure which characters are causing the problem.
This is the js I'm trying but gives me and an "invalid quanitifier" error on the first line
var myregex = /storify.com\/*(?<code>[^"]*)/;
var storify = 'http://storify.com/DigitalFirst/ces-2013-five-things-you-missed-day-3';
var remoteid = storify.match(myregex);
console.log(remoteid);
Thanks in advance for anyone who can help get this working.
JavaScript regexes have no suport for named captures (?<xxx>...), that is why.
More details here (note: JavaScript is referred to as ECMA [262, to be precise]).
Related
I have the following url
https://myurl/blogs/<blog-category>/<blog-article>
I've trying to create a regEx so i can thrigger a script only when i'm in an article.
i tried this among other tests but it didn't work and i'm not really the best guy building RegExs.
window.location.pathname.match(/\/blogs\/^[a-zA-Z0-9_.-]*$\/^[a-zA-Z0-9_.-]*$/
So in my understanding the first part of this regEx (\/blogs\/) is trying just to match a fixed string.
Then next parts just tries to match any kind of numeric,character and _.- combination (which is basically the potential strings that i can have there)
However this is not working at all.
My piece of script is looking like this
if(window.location.pathname.match(/\/blogs\/^[a-zA-Z0-9_.-]*$\/^[a-zA-Z0-9_.-]*$/){
// A code implementation here
}
Note: One thing that i noticed when writing this is that if i remove everything and just try
window.location.pathname.match(/\/blogs\/)
It doesn't work either.
Can someone help me solve this? I will also appreciate any guide that can help me improve my RegEx skills.
Thanks!
Update: to have this working i had to separate my condition into two things to get it to work properly.
It ended up looking like this:
var path = window.location.pathname;
const regEx = /\/blogs\/[a-zA-Z0-9_.-]*\/[a-zA-Z0-9_.-]*/i;
if(path.match(regEx)){
// My code here
}
This should work:
\/blogs\/[a-zA-Z0-9_.-]*\/[a-zA-Z0-9_.-]*
the "^" symbol checks that it is the start of a string which is not the case for the url in question
I would suggest using https://regexr.com/ for testing your regex to remove any other possible issues from other code
var patt = /\/blogs\/[a-zA-Z0-9_.-]*\/[a-zA-Z0-9_.-]*/i window.location.pathname.match(patt)
You can try using this
I have the following set of code
var nameStartChar = /[A-Z_a-z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]///\u10000-\uEFFFF
var nameChar = new RegExp("[\\-\\.0-9"+nameStartChar.source.slice(1,-1)+"\u00B7\u0300-\u036F\\ux203F-\u2040]");
var tagNamePattern = new RegExp('^'+nameStartChar.source+nameChar.source+'*(?:\:'+nameStartChar.source+nameChar.source+'*)?$');
It throws the following error:
sqmtest I/JS: SyntaxError: Invalid regular expression: /[\-\.0-9A-Z_a-z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD????-??\ux203F-???]/: Range out of order in character class
at new RegExp (<anonymous>)
at RegExp (<anonymous>)
at Object.$$_sax (http://com.hashcube.sqtest/modules/devkit-core/src/clientapi/native/dom/sax.js:1:5952)
at I (none:615:5092)
at z (none:615:6690)
at Object.jsio (none:615:7357)
at Object.$$_dom_parser (http://com.hashcube.sqtest/modules/devkit-core/src/clientapi/native/dom/dom_parser.js:1:3511)
at I (none:615:5092)
at z (none:615:6690)
at Object.jsio (none:615:7357)
at Object.$$_dom_DOMParser (http://com.hashcube.sqtest/modules/devkit-core/src/clientapi/native/dom/DOMParser.js:1:66)
at I (none:615:5092)
at z (none:615:6690)
at Object.jsio (none:615:7357)
at Object.$$_common.exports.install (http://com.hashcube.sqtest/modules/devkit-core/src/clientapi
The full code is here -> https://github.com/hashcube/devkit-core/blob/hc/src/clientapi/native/dom/sax.js
Any idea why the regex could be failing.
I have not confirmed this, but looks like minification could be causing an issue.
Any thoughts suggestions would help me. I can provide more details if needed
EDIT
I have more information. I looked at the minifed code for both these lines from 2 machines(1 machine where I get this error and another where I don't get the error). It looks like an encoding issue to me. Any help would be appreciated
Machine with Error
var nameStartChar=/[A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]/,
nameChar=RegExp("[\\\\-\\\\.0-9"+nameStartChar.source.slice(1,-1)+"????-??\\\\ux203F-???]"),
tagNamePattern=RegExp("^"+nameStartChar.source+nameChar.source+"*(?::"+nameStartChar.source+nameChar.source+"*)?$"),S_TAG=0,S_ATTR=1,S_ATTR_S=2,S_EQ=3,S_V=4,S_E=5,S_S=6,S_C=7;
Machine without error
var nameStartChar=/[A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]/,
nameChar=RegExp("[\\\\-\\\\.0-9"+nameStartChar.source.slice(1,-1)+"·�~#-ͯ\\\\ux203F-�~A~#]"),]"),A~#
tagNamePattern=RegExp("^0-9"+nameStartChar.sou+nameChar.source+"*(?::"+nameStartChar.source+nameChar.source+"*)?$"),S_TAG=0,S_ATTR=1,S_ATTR_S=2,S_EEouQ=3,S_V=4,S_E=5,S_S=6,S_C=7;
Seems like you are providing ranges that are not really range for Regex like;
????-??\\\\ux203F-??? => ????-??\\ux203F-???
\\ux203F-??? This is not a range for regex and causes the problem. Rest seems like correct.
Regex accept this one ·�~#-ͯ\\\\ux203F-�~A~#, you can use this if it works for you.
Check Character ranges link, this might help you restructure your regex.
PS: "Machine without error" returns "Unexpected token ]"
I am using the QuickBlox JavaScript API. Looking through their code, I found this line:
var URL_REGEXP = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
It appears that it has declared a string variable that is a regular expression pattern. Then it goes ahead to use that variable thus:
return str.replace(URL_REGEXP, function(match) {
url = (/^[a-z]+:/i).test(match) ? match : 'http://' + match;
url_text = match;
return '' + escapeHTML(url_text) + '';
});
I am wondering how is this possible? The var declared in the first line should be a string, but it is unquoted. Shouldn't this be a syntax error?
I went ahead and tested this code on my browser, and it works! This mean's I've got some learning to do here... Can anyone explain how this variable is declared?
Additionally, I tried to run the same code on my friends computer, the Chrome debugger throws a syntax error on the variable declaration line (unexpected token '/'). I am using Chrome Version 36.0.1985.143 m, my friend is using the same thing, but on my computer, it all works fine, on my friends computer, the code stops at the first variable declaration because of "syntax error".
Is there some setting that is different?
Any help would be appreciated.
UPDATE
Thanks for the quick answers. I've come from a PHP background, so thought that all regular expressions has to be initialized as strings :P.
Anyone can reproduce the syntax error I'm getting on my friends computer? (It still happens after disabling all extensions). I can't reproduce it either, and that's what is frustrating me.
UPDATE 2
I have tested and my friends computer and looked through the source. It appear to be due to some encoding problems (I'm not sure what). The regular expression is shown like this:
var URL_REGEXP = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?芦禄鈥溾€濃€樷€橾))/gi;
(The characters at the end of the code is some random chinese characters, it seems).
How can I change the encoding to match his browser/system? (He is running on a Windows 7 Chinese simplified system).
It is not a String variable. It is a regular expression.
Calling var varname = /pattern/flags;
is effective to calling var varname = new RegExp("pattern", "flags");.
You can execute the following in any browser that supports a JavaScript console:
>>> var regex = /(?:[\w-]+\.)+[\w-]+/i
>>> regex.exec("google.com")
... ["google.com"]
>>> regex.exec("www.google.com")
... ["www.google.com"]
>>> regex.exec("ftp://ftp.google.com")
... ["ftp.google.com"]
>>> regex.exec("http://www.google.com")
Anyone can reproduce the syntax error I'm getting on my friends computer? (It still happens after disabling all extensions). I can't reproduce it either, and that's what is frustrating me.
According to RegExp - JavaScript documentation:
Regex literals was present in ECMAScript 1st Edition, implemented in JavaScript 1.1. Use an updated browser.
No, it shouldn't be a syntax error. In Javascript, RegExp objects are not strings, they are a distinct class of objects. /.../modifiers is the syntax for a RegExp literal.
I can't explain the syntax error you got on your friend's computer, it looks fine to me. I pasted it into the Javascript console and it was fine.
I am trying to get a regular expression to remove some junk on a string that I am getting back from a service. The string has some phone data that I want to remove. An example string is "en-us; droid4 build/9.8.2o-72_vzw-18" . My first task is to drop the "build/" to the end of the string or to the end of word (not sure which is better in my scenario).
I ended up with:
var deviceRegExpr = new RegExp("\b(build\/\S*)");
First off, I am not sure if this is the best way to accomplish this. But, when I am looking at the Chrome debugger tools the regExp value is showing up as "/(build/S*)/" . I cant figure out what is happening to the \b ?
Thanks in advance for any help.
This String#replace(regex, repacement) should work:
var repl = "en-us; droid4 build/9.8.2o-72_vzw-18".replace(/\s*\bbuild\/.*$/, "");
//=> en-us; droid4
I'm trying to search plain old strings for urls that begin with http, but all the regex I find doesn't seem to work in javascript nor can I seem to find an example of this in javascript.
This is the one I'm trying to use from here and here:
var test = /\b(?:(?:https?|ftp|file)://www\.|ftp\.)[-A-Z0-9+&##/%=~_|$?!:,.]*[A-Z0-9+&##/%=~_|$]/;
But when I try to run it, I get "Unexpected token |" errors.
Ok, a comment seems to be not enough, hard to find full answer. I rewrite whole proper regexp: (tested, it works good)
var test = /\b(?:(?:https?|ftp|file):\/\/www\.|ftp\.)[-A-Z0-9+&##\/%=~_|$?!:,.]*[A-Z0-9+&##\/%=~_|$]/i;
The i on the end means 'ignore case', so it is necessary for this regexp.
You're using / as your regex delimiter, and are also using / within the regex (before www), so the regex actually terminates after the first / before www. Change it to:
var test = /\b(?:(?:https?|ftp|file):\/\/www\.|ftp\.)[-A-Z0-9+&##/%=~_|$?!:,.]*[A-Z0-9+&##/%=~_|$]/;
^^^^ escape here