Javascript unrecognized expression - javascript

i have complex element attributes like
CS::#station1/cs1_station-0/be/PA300___(1)#22
I tried to remove all expressions with this regex
/[/\#\/\_\/\#\/\:\/\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g
but i get exceptions like:
Error: Syntax error, unrecognized expression: #OS::#station1\cs-0...
Has somebody a regex to escape all unrecognized js expressions?

/[/\#\/\_\/\#\/\:\/\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g
should probably be
/[-\/#_#:[\]{}()*+?.\\^$|]+/g

Perhaps a negated character class may be useful:
var s = 'CS::#station1/cs1_station-0/be/PA300___(1)#22';
console.log(s.replace(/[^a-z0-9]+/gi, ''));

Related

Jquery Syntax error, unrecognized expression on attribute selector

I have html div with one attribute.
div exp_attribute="{"prop_val_name":1}">mydiv</div>
I tried to trigger click on it via jQuery like
$('div[exp_attribute={"prop_val_name":1}]').click(); // not working
$('div[exp_attribute=\\{"prop_val_name":1\\}]').click(); // not working with escaping special chars
$('div[exp_attribute=\\{\\"prop_val_name\\":1\\}]').click(); // not working with escaping more special chars
but keep getting error
Error: Syntax error, unrecognized expression: div[exp_attribute=\{\"prop_val_name\":1\}]
so any idea how to handle the issue?
Better to escape everything
$('div[exp_attribute=\\{\\"prop_val_name\\"\\:1\\}]').click();

Trying to escape a string to use as a jQuery selector

I have the following code, which I stole from another SO question,
$('#'+'^`test'.replace(/[!"#$%&'()*+,.\/:;<=>?#[\\\]^`{|}~]/g, "\\\\$&"))
which produces the following error.
Uncaught Error: Syntax error, unrecognized expression: #\\^\\`test(…)
I just have some IDs with crazy characters, like ^ and `, that I need jQuery to not choke on. I don't get why that error is happening, because if I manually add the slashes into the string like,
$('#'+'\\^\\`test')
then it works fine. What's wrong with the regex method?
I just have some IDs with crazy characters, like ^ and `, that I need jQuery to not choke on.
By far the simplest way to address that is with an attribute selector:
$('[id="' + theId + '"]').doSomething();
As long as theId doesn't contain backslashes or double quotes, no need for further escaping.
Another work around is to use the vanilla getElementById, which doesn't parse the selector. That way you still have the efficiency of selecting by id:
let res = $(document.getElementById('^`test'));
How about using this?
const $ID = function(selector){
return $('[id="' + selector.match(/#?(\S+)/)[1] + '"]')
}
Then you can use it just like jquery
$ID('#^`div')
You need 2 times the escape . Becouse first the replace/regex need the escape to write a escape. The next escape is need from jquery by $().
More clear syntax as the postet regex is:
"#^bla`".replace('^','\\\^').replace('`','\\\`');
will replace "#^bla`" to "#\\^bla\\`".
Uncaught Error: Syntax error, unrecognized expression: #\\^\\`test(…)
If you want to use your regex you must also escape [ ] with \[ and \].
"+".replace(/[!"#$%&'()*+,.'()*+,.\/:;<=>?#\[\\\]^`{|}~]/g, "yes")

How to make a regex call with a '&' or '?' as first character

I am trying to make a regex call with the following string:
if ((/&timeout=true|?scn=header/i).test(url))
... and the statement continues. I am receiving this error in the console:
Uncaught SyntaxError: Invalid regular expression: /&timeout=true|?scn=header/: Nothing to repeat
How do I properly make this statement?
The problem is that ? has a special meaning of repeat zero or one times, hence the error. Just escape it:
/&timeout=true|\?scn=header/
You will have to escape the question mark character as it is a regex operator:
if ((/&timeout=true|\?scn=header/i).test(url))
Try /(&timeout=true|\?scn=header)/i
? is a special character in regex so you need to escape it
Demo: http://www.regexr.com/3bqlu
Try this:
/[&\?]timeout=true|[&\?]scn=header/
It matches both
http://example.com/file.html?scn=header&timeout=true
http://example.com/file.html?timeout=true&scn=header
because we don't know which parameter comes first
This seems to work
/\&timeout=true|\/?scn=header/i

Jquery Syntax error, unrecognized expression on id selector

Im using angular and jquery to scroll to an element base on his location hash string.
In my situation i need to include in the string the '?' char, but its seems like jquery has problem with this.
This is the link:
when Are Lottery Results Updated OnThe Site
This is the jquery code:
var elem = '#' + $location.hash();
console.log($(elem));
The error:
Error: Syntax error, unrecognized expression: #whenAreLotteryResultsUpdatedOnTheSite?
Any solution?
Yes, jQuery will refuse to select elements with special characters in CSS selector. You just need to escape them with \\:
var elem = $location.hash().replace(/\?/, '\\\\?');
This will properly escape ? character.
Also note that location.hash will already include leading # so you don't need to prepend one more.

Regex: Replace Parentheses in JavaScript code with Regex

Need to remove spaces, single quotes, and parens from a string displayed in as an <li>
This works great for the first two:
var foo = li.replace(/\s|\'+/g, "");
I assumed all I needed to do was add another few ORs to this, escaping the ( or ) symbols to search for them:
var foo = li.replace(/\s|\'+/g|\(|\), "");
Compiler no likey:
Uncaught SyntaxError: Unexpected token ILLEGAL
It appears the the that the open and closing paren are getting evaluated anyway - even with the escape symbols. What am I doing wrong here? Thanks!
You don't have the right syntax for regex literals. They're written as
/pattern/flags
And what you seem to want isn't a bunch of OR but a character class.
Use
var foo = li.replace(/[\s'()]+/g, "");

Categories