dynamically creating a regex with forward slashes in javascript - javascript

Im trying to match lines that have this format. Writing a static regEx is fine but I need to do this using 2 variables to create the regex dynamically.
I cant seem to get how to escape the forward brackets properly ive tried escaping them, not escaping them and even double escaping them (just for the heck of it) but fireBug shows the actual regEx being created the same no mater how I do itand it doesnt match my input.
Lines to match look like this:
9.SSRDOCSWSHK1/////23NOV96/M//YEUNG/WINSTON/JEREMY-5.1
What Ive tried:
var regString ='\\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[/]{5}\\d\\d[A-Z]{3}\\d\\d/[MF]//'+curGstNme+'([/A-Z]+)?-'+pax.slice(0,1)+'\.'
var namedGdocRegEx = new RegExp(regString,"g");
FireBug gives RegExp /\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\d\d[A-Z]{3}\d\d\/[MF]\/\/CASTANEDA\/HAZEL([\/A-Z]+)?-1./g
---------------------------
var regString ='\\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\\d\\d[A-Z]{3}\\d\\d\/[MF]\/\/'+curGstNme+'([\/A-Z]+)?-'+pax.slice(0,1)+'\.'
var namedGdocRegEx = new RegExp(regString,"g");
FireBug gives RegExp /\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\d\d[A-Z]{3}\d\d\/[MF]\/\/CASTANEDA\/HAZEL([\/A-Z]+)?-1./g
---------------------------
var regString ='\\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\\/]{5}\\d\\d[A-Z]{3}\\d\\d\\/[MF]\\/\\/'+curGstNme+'([\\/A-Z]+)?-'+pax.slice(0,1)+'\.'
var namedGdocRegEx = new RegExp(regString,"g");
FireBug gives RegExp /\d{1,2}.SSRDOCS[0-9A-Z]{2}HK1[\/]{5}\d\d[A-Z]{3}\d\d\/[MF]\/\/CASTANEDA\/HAZEL([\/A-Z]+)?-1./g

In regex you need to escape the DOTs since DOT will mean any character.
Use this regex:
regString ='\\d{1,2}\\.SSRDOCS[0-9A-Z]{2}HK1/{5}\\d\\d[A-Z]{3}\\d\\d/[MF]//'+ curGstNme + '([/A-Z]+)?-' + pax.slice(0,1) + '\\.';

Actually the problem was elsewhere. Apparently fireBug just shows the created regex (in the "watch" panel) with the escaping sashes visible. This made me think the regex was not being created correctly.

Your regex could be reduced like this:
var regString ='\\d{1,2}\\.SSRDOCS[0-9A-Z]{2}HK1/{5}\\d{2}[A-Z]{3}\\d{2}/[MF]//'+curGstNme+'([/A-Z]+)?-'+pax.slice(0,1)+'\.'
What did I change ?
\\d\\d can be replaced efficiently with \d{2}.
. replaced with \\. for matching exactly dot.
[/]{5} replaced by /{5}
This visually gives you:

Related

Regex: get string between last character occurence before a comma

I need some help with Regex.
I have this string: \\lorem\ipsum\dolor,\\sit\amet\conseteteur,\\sadipscing\elitr\sed\diam
and want to get the result: ["dolor", "conseteteur", "diam"]So in words the word between the last backslash and a comma or the end.
I've already figured out a working test, but because of reasons it won't work in neitherChrome (v44.0.2403.130) nor IE (v11.0.9600.17905) console.There i'm getting the result: ["\loremipsumdolor,", "\sitametconseteteur,", "\sadipscingelitrseddiam"]
Can you please tell me, why the online testers aren't working and how i can achieve the right result?
Thanks in advance.
PS: I've tested a few online regex testers with all the same result. (regex101.com, regexpal.com, debuggex.com, scriptular.com)
The string
'\\lorem\ipsum\dolor,\\sit\amet\conseteteur,\\sadipscing\elitr\sed\diam'
is getting escaped, if you try the following in the browser's console you'll see what happens:
var s = '\\lorem\ipsum\dolor,\\sit\amet\conseteteur,\\sadipscing\elitr\sed\diam'
console.log(s);
// prints '\loremipsumdolor,\sitametconseteteur,\sadipscingelitrseddiam'
To use your original string you have to add additional backslashes, otherwise it becomes a different one because it tries to escape anything followed by a single backslash.
The reason why it works in regexp testers is because they probably sanitize the input string to make sure it gets evaluated as-is.
Try this (added an extra \ for each of them):
str = '\\\\lorem\\ipsum\\dolor,\\\\sit\\amet\\conseteteur,\\\\sadipscing\\elitr\\sed\\diam'
re = /\\([^\\]*)(?:,|$)/g
str.match(re)
// should output ["\dolor,", "\conseteteur,", "\diam"]
UPDATE
You can't prevent the interpreter from escaping backslashes in string literals, but this functionality is coming with EcmaScript6 as String.raw
s = String.raw`\\lorem\ipsum\dolor,\\sit\amet\conseteteur,\\sadipscing\elitr\sed\diam`
Remember to use backticks instead of single quotes with String.raw.
It's working in latest Chrome, but I can't say for all other browsers, if they're moderately old, it probably isn't implemented.
Also, if you want to avoid matching the last backslash you need to:
remove the \\ at the start of your regexp
use + instead of * to avoid matching the line end (it will create an extra capture)
use a positive lookahead ?=
like this
s = String.raw`\\lorem\ipsum\dolor,\\sit\amet\conseteteur,\\sadipscing\elitr\sed\diam`;
re = /([^\\]+)(?=,|$)/g;
s.match(re);
// ["dolor", "conseteteur", "diam"]
You may try this,
string.match(/[^\\,]+(?=,|$)/gm);
DEMO

javascript regex invalid quantifier error

I have the following javascript code:
if (url.match(/?rows.*?(?=\&)|.*/g)){
urlset= url.replace(/?rows.*?(?=\&)|.*/g,"rows="+document.getElementById('rowcount').value);
}else{
urlset= url+"&rows="+document.getElementById('rowcount').value;
}
I get the error invalid quantifier at the /?rows.*?.... This same regex works when testing it on http://www.pagecolumn.com/tool/regtest.htm using the test string
?srt=acc_pay&showfileCL=yes&shownotaryCL=yes&showclientCL=no&showborrowerCL=yes&shownotaryStatusCL=yes&showclientStatusCL=yes&showbillCL=yes&showfeeCL=yes&showtotalCL=yes&dir=asc&closingDate=12/01/2011&closingDate2=12/31/2011&sort=notaryname&pageno=0&rows=anything&Start=0','bodytable','xyz')
In this string, the above regex is supposed to match:
rows=anything
I actually don't even need the /? to get it to work, but if I don't put that into my javascript, it acts like it's not even regex... I'm terrible with Regex period, so this one has me pretty confused. And that error is the only one I am getting in Firefox's error console.
EDIT
Using that link I posted above, it seems that the leading / tries to match an actual forward slash instead of just marking the code as the beginning of a regex statement. So the ? is in there so that if it doesn't match the / to anything, it continues anyway.
RESOLUTION
Ok, so in the end, I had to change my regex to this:
/rows=.*(?=\&?)/g
This matched the word "rows=" followed by anything until it hit an ampersand or ran out of text.
You need to escape the first ?, since it has special meaning in a regex.
/\?rows.*?(?=\&)|.*/g
// ^---escaped
regtest.htm produces
new RegExp("?rows.?(?=\&)|.", "") returned a SyntaxError: invalid
quantifier
The value you put into the web site shouldn't have the / delimiters on the regex, so put in ?rows.*?(?=\&)|.* and it shows the same problem. Your JavaScript code should look like
re = /rows.*?(?=\&)|.*/g;
or similar (but that is a pointless regex as it matches everything). If you can't fix it, please describe what you want to match and show your JavaScript
You might consider refactoring you code to look something like this:
var url = "sort=notaryname&pageno=0&rows=anything&Start=0"
var rowCount = "foobar";
if (/[\?\&]rows=/.test(url))
{
url = url.replace(/([\?\&]rows=)[^\&]+/g,"$1"+rowCount);
}
console.log(url);
Output
sort=notaryname&pageno=0&rows=foobar&Start=0

Javascript .test either returns nothing or false, when regex matches the tested string?

I'm using javascript's inbuilt .test() function to test whether a string or regex matches. I took the regex from RegexLib, so I know that it matches the string it's tested against (in this case joe#aol.com), however it either returns false or nothing at all.
Here's my code:
var string = "joe#aol.com";
var pattern = [\w-]+#([\w-]+\.)+[\w-]+/i;
var match = pattern.test(string);
document.write(match);
When the regex is encased in quotes, the test returns false, when it's not encased in anything, it returns nothing.
What I've tried so far:
Simply using a single line, var match = '[\w-]+#([\w-]+\.)+[\w-]+/i'.test("joe#aol.com");.
Using both ' single quotes and " double quotes for both regex and string.
Appending the regex with /i and /g.
I honestly don't know what's causing this issue, so any help would be great. It could be a complete rookie mistake, a forgotten syntax perhaps.
Here's a link to the jsFiddle I made up for you to play around with if you think you've got some idea of how to fix this up: http://jsfiddle.net/wFhEJ/1/
You missed the opening slash for a regexp. The notation for a regexp is:
/regexp/flags
What happened with enclosing the regexp is that it became a string, and on jsFiddle String.prototype.test has been set by MooTools. MooTools seems to provide a String.prototype.test function, but it's not the same as RegExp.prototype.test.
http://jsfiddle.net/wFhEJ/2/
var string = "joe#aol.com";
var pattern = /[\w-]+#([\w-]+\.)+[\w-]+/i;
var match = pattern.test(string);
document.write(match);
Do note though that document.write is frowned upon. You might rather want document.body.appendChild(document.createTextNode(match)) or something alike.

URL regex does not work in javascript

I am trying to use John Gruber's URL regex in Javascript but NetBeans keeps telling me there is a syntax error and illegal errors:
var patt = "/(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])
|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]
{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|
(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|
(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:
'".,<>?«»“”‘’]))/";
Anyone know how to solve this?
As others have said, it's the double quote. But alternatively, you can just write the regexp as a literal in javascript (but then you need to escape the forward slashes in lines 1 and 3 instead).
var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i;
I also moved the case-insensitive modifier to the end. Just because. (edit: Well, not just "because" - see Alan Moore's comment below)
Note: Whether you use a literal or a string, it has to be on 1 line.
put the whole expression in one line, and remove the quotes at the start and end so it looks like this var patt = /the-long-patttern/;, netbeans will still complain, but the browsers won't and thats what matters.
You should write it like this in NetBeans:
"(?i)\\b((?:[a-z][\\w-]+:(?:\\/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]"
+ "+[.][a-z]{2,4}\\/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))"
+ "+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))";

Javascript storing regex in a variable

I have this line of code in javascript
var re = (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?
Usually I encapsulate the regex syntax with the / characters but since they are found within the regex it screws up the encapsulation. Is there another way how I can store it inside the variable?
The current slashes that seem like escape characters are part of the regex, since I am using this in c# aswell and works perfectly
var re = new RegExp("^your regexp.*$", "gi");
One way is to escape all occurances of / in your regex as \/, like you're already partially doing:
var re = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:\/~\+#]*[\w\-\#?^=%&\/~\+#])?/;
You can escape the slash inside your regex:
/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:\/~\+#]*[\w\-\#?^=%&\/~\+#])?/
(you already did so with the first two slashes...)

Categories

Resources