javascript regex error - javascript

I have this string:
£1,134.00 (£1,360.80 inc VAT)
And I am trying to extract the numbers to get the following:
['1,134.00','1,360.80']
Using the following regex pattern in Javascript:
/\d*,?\d+\.\d{2}/g
It is working fine in Chrome, but I get this error in Opera:
Uncaught exception: Syntax error, unrecognized expression: (£1,360.80 inc VAT)
Error thrown at line 75, column 784 in <anonymous function: k.error>(g) in http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js:
throw"Syntax error, unrecognized expression: "+g;
Obviously I would like it to work in all modern browsers but I have no idea what is causing this. I have also tried several other regex patterns and have looked into escape characters as I thought it might be that.
Any ideas?!
Let me know if more info is needed. Thanks

unrecognized expression: (£1,360.80 inc VAT) <= that's not an error in your regex. Your string is not being a string. Somehow it's getting mixed and interpreted as part of your javascript.

Related

Using JavaScript Regex to replace some content I get a Unmatched ')' Regex error

I'm moving a large blog from WordPress to another platform, and I am trying to re-create WordPress shortcodes with Javascript.
I've been able to write the Regex to replace [youtube]'s shortcodes without problems, but I'm having problems with [soundcloud]'s.
The format of the shortcode is the following:
[soundcloud url=”http://api.soundcloud.com/tracks/33660734”]
I created the following Regex rule, that seems to work on Regex 101
\[soundcloud url="http(s?):\/\/api\.soundcloud\.com\/tracks\/([a-zA-Z0-9]*)"(\s*?)\]
https://regex101.com/r/JKL45q/2
But when I include it in my script:
{
service: 'soundcloud',
regex: new RegExp('\[soundcloud url="http(s?):\/\/api\.soundcloud\.com\/tracks\/([a-zA-Z0-9]*)"(\s*?)\]', 'ig'),
},
I get this error, which I can't understand:
main.js:29 Uncaught SyntaxError: Invalid regular expression: /[soundcloud url="http(s?)://api.soundcloud.com/tracks/([a-zA-Z0-9]*)"(s*?)]/: Unmatched ')' (at main.js:29:14)
at new RegExp (<anonymous>)
at main.js:29:14
at main.js:142:3
The source script is here:
https://hotmc.pages.dev/assets/js/main.js
This is a page where the error occurs in the console:
https://hotmc.pages.dev/2011/01/10/esclusivo-anteprima-album-micha-soul-un-brano-in-free-download
Can anyone help? I found other questions about this error, but haven't been able to apply the suggested fixes.
Thank you in advance,
S.
I think you can remove the () of the ([a-zA-Z0-9]*)
new RegExp('[soundcloud url="http(s?):\/\/api\.soundcloud\.com\/tracks\/[a-zA-Z0-9]*"(\s*?)]', 'ig').test(`[soundcloud url="http://api.soundcloud.com/tracks/33660734"]`)
I have been able to get rid of the error by changing hot the RegExp object is initialized.
From this:
new RegExp('\[soundcloud url="http(s?):\/\/api\.soundcloud\.com\/tracks\/([a-zA-Z0-9]*)"(\s*?)\]', 'ig')
To this:
new RegExp(/\[soundcloud url="http(s?):\/\/api\.soundcloud\.com\/tracks\/([a-zA-Z0-9]*)"(\s*?)\]/, 'ig')

JavaScript minification fails containing certain Regex

I can't minify a javascript file containing this working Regex:
isValid = function (str) {
return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
},
Error message:
/* Minification failed. Returning unminified contents.
(2128,43-44): run-time error JS1004: Expected ';': {
(2128,45-46): run-time error JS1195: Expected expression: |
(2128,46-47): run-time error JS1014: Invalid character: \
(2128,47-48): run-time error JS1014: Invalid character: \
(2128,48-70): run-time error JS1015: Unterminated string constant:
":<>\?]/g.test(str);
(2129,6-7) : run-time error JS1195: Expected expression: ,
(2128,17-43): run-time error JS5017: Syntax error in regular expression:
/[~`!#$%\^&*+=\-\[\]\\';,/
*/
Bundle Config:
bundles.Add(new ScriptBundle("~/bundlesJs/custom").Include(
"~/Scripts/Autogenerated/AntiHacking/antiHackingHelper.js"
));
BundleTable.EnableOptimizations = true;
How can this be solved? Is there a character that needs to be escaped or should I wrap the whole Regex?
Acc. to ES5 docs, the forward slash is NOT a special regex metacharacter.
JavaScript regex engine always allows an unescaped / in a RegExp constructor notation and you may use an unescaped / forward slash inside character classes even when written in a regex literal notation, try this code (in Chrome, FF, and I have checked it in IE 11, too):
console.log( '1//25///6'.match(/\w[/]+\w/g) );
console.log( '1//25///6'.match(RegExp("\\w/+\\w", "g")) );
However, the best practice is to escape the regex delimiters to avoid any issues like the one you have and keep the patterns consistent and unambiguous.

Getting Lexer Error with AngularJS and ng-pattern

I'm getting the following error at runtime:
Error: [$parse:lexerr] Lexer Error: Unexpected next character at columns 11-11 [\] in expression [[0-9]{1,4}(\.[0-9]{1,3})?].
My HTML looks like this
<input type="text"
class="form-control"
name="amount"
ng-pattern="{{ctrl.pattern}}"
ng-model="ctrl.amount">
The assignment in my AngularJS controller is as follows:
ctrl.pattern = '[0-9]{1,4}(\\.[0-9]{1,3})?';
From what I understand AngularJS appends the ^ and $ at the beginning and end of regular expressions and that works great. The problem is that the regex literal in the middle of the expression is not being accepted. I want to dot to be accepted as a literal and not as any character so I need a way to escape it yet the lexer does not to like it. Is there a way around this?
This issue is already reported here but they are not going to fix it as according to them its a uncommon usecase.
Do it this way instead:
ctrl.pattern = /^[0-9]{1,4}([.][0-9]{1,3})?$/;
This way the regex will be evaluated as a regex object instead of a string parameter to RegExp in which case we will need to add ^,$

Javascript Regex Issue

I'm trying to build a regex to test if the referrer contains a certain url (but includes query parameters etc)
Apologies if it's bad but it's my first attempt:
(ftp|http|https):\/\/urlhere\.com\/directoryhere(\?|/[^-+(a-zA-Z)]|$)
It works perfectly on here:
http://gskinner.com/RegExr/
But when I try it in in JavaScript/Firebug using:
document.referrer.match(/(ftp|http|https):\/\/urlhere\.com\/directoryhere(\?|/[^-+(a-zA-Z)]|$)/gi);
I get the error:
SyntaxError: unterminated parenthetical
Any help appreciated.
There is a / which is unescaped after directoryhere - (\?|/[
(ftp|http|https):\/\/urlhere\.com\/directoryhere(\?|\/[^-+(a-zA-Z)]|$)
You have missed escape one / before [^-+(a-zA-Z)]
Try
document.referrer.match(/(ftp|http|https):\/\/urlhere\.com\/directoryhere(\?|\/[^-+(a-zA-Z)]|$)/gi);

Parsing XFN data with Jquery, round 2

You guys already helped me on correctly parsing the REL attribute on A tags, but there are two XFN values that I'm not able to match:
"co-worker" and "co-resident". The hyphen causes an error with jquery.
I tried this
xfn_co-worker = $("a[rel~='co-worker']").length;
and this
xfn_co-worker = $("a[rel~='co\-worker']").length;
In both cases the error "Uncaught ReferenceError: Invalid left-hand side in assignment" is returned.
(Being these standard XFN values, I'm forces to use them)
Any idea is appreciated, as usual :-)
This isn't an error in you selector. The error lies in your variable name.
You can't use mathematical operators in the variable name. So the problem is your use of the - sign.
Try replacing
xfn_co-worker
with e.g
xfn_co_worker
And it should work alright
xfn_co_worker = $("a[rel~='co-worker']").length;
Note: Your variable name must match the following regex [a-zA-Z_$][0-9a-zA-Z_$]*

Categories

Resources