Regular expression alternative for javascript escape() function - javascript

I'm working on Jmeter, and I need to send an encoded parameter along with the Http request.
I know that I can do the encoding of special characters using javascript escape(). But I can't use javascript here, as I'm using Jmeter's Regular Expression Extractor. I need a regular expression pattern that does the same as escape(). Please do help me. Thanks in advance.

New upcoming version 2.10 of jmeter will have a new function that does it:
https://issues.apache.org/bugzilla/show_bug.cgi?id=54991

Regular expressions can't do this. escape() (which is deprecated anyway and has been superseded by encodeURI()) takes ASCII control characters and non-ASCII characters and encodes them using %xx or %uxxxx hexadecimal notation. Regular expressions can only work with existing text, not convert it.

Related

using same regex both in php and javascript

Problem:-
I am using the following regex to find the special characters in a string
"/[^a-zA-Z0-9\s]/i"
I want to get all the characters that match this pattern and all that is working fine.
The condition is that that I have to use the same expression both in php and javascript.
But the g flag in the above regex is creating problem as preg_match and preg_match_all do not accept this flag and I have to search globally.
Question:-
SO how can I get all the special characters using the same expression both in php and javascript?
You can't use the same regex in both PHP and JavaScript because their regex engines make different assumptions and support different features.
More than just the incompatibility with the g modifier, this regex will fail you if the input contains non-ASCII characters: the input encoding in PHP and JS will be almost certainly different and PHP will not even be Unicode-aware unless you use the u flag (which does not exist in JS because it's Unicode-aware by default).
Just use two different regular expressions.
To match [^a-zA-Z0-9\s] in JavaScript you would have to use:
[\u0000-\u0008\u000F-\u001F\u0022-\u002F\u003B-\u0040\u005C-\u0060\u007C-\u0084\u0087-\u009F\u00A2-\u167F\u1682-\u180D\u1810-\u1FFF\u200C-\u2027\u202B-\u202E\u2031-\u205E\u2061-\u2FFF\u3002-\uFFFF]

I have problems in use unicode in java scriptn

I'm beginner in Javascript, I have a url containing unicode like below:
/Solutions/راه-کار-جامع-امنیت-اطلاعات
Now I need to read the path name by following code
window.location.pathname.split('/')
and in the output I have this
"", "Solutions", "%D8%B1%D8%A7%D9%87-%DA%A9%D8%A7%D8%B1-%D8%AC%D8%A7…%D8%AA->%D8%A7%D8%B7%D9%84%D8%A7%D8%B9%D8%A7%D8%AA"
How can I solve this problem?
The unicode text is url-encoded. This means that the unicode characters are translated to codes that are safe to use as a url. You can revert this using the decodeURIComponent or the decodeURI function.
The difference between these two is already nicely explained in this question. In your case you will most likely use decodeURIComponent after you performed the split.

Best way to encode special chars in url

What is the best way to encode special characters in a url? Let's say I pass an variable from javascript to a php script that way:
http://example.com/my sp3c!al var!a$le
What is the best way to encode that special characters (like whitespace, !, $, /, \ etc.)? Is there a method in javascript to encode it with a corresponding function in php to decode it there?
You need to make use of encodeURIComponent(yourvar);
You can do it in three ways
encodeURI(yourvar)
output http://example.com/my%20sp3c!al%20var!a$le
amd
encodeURIComponent(yourvar)
output http%3A%2F%2Fexample.com%2Fmy%20sp3c!al%20var!a%24le
escape()
output http%3A//example.com/my%20sp3c%21al%20var%21a%24le
using escape is not recomended because it is deprecated since ECMAScript v3.
functions is for JavaScript escaping, not HTTP.

JavaScript Regular Expression Syntax

I am trying to create a regular expression that checks for letters, numbers, and underscores. In .NET, I can do "^\w+$". However, I am not that familiar with the JavaScript syntax. Can somebody help me out?
Thank you!
One obvious difference is that in JavaScript, you write the regex as /pattern/flags -- this is Perl-style. Your "example" would then be ^\w+$ → /^\w+$/.
For example, replace multiple e's with one e, case-insensitive search (hence the i flag):
var s='qweEEerty';
s=s.replace(/e+/i, 'e');
Returns: qwerty.
That same expression will work in JavaScript (there are some differences between .NET regular expressions and JavaScript regular expressions but not in this example).
I recommend that you read Using Regular Expressions with JavaScript and ActionScript to learn a bit more about JavaScript's regular expression implementation.

How to find a URL within full text using regular expression

What is wrong with the following regular expression, which works in many online JavaScript regular expression testers (and RegEx Buddy), yet doesn't work in my application?
It is intended to replace URLs with a Hyperlink. The Javascript is found in a javascript file.
var fixed = text.replace(/\b(https?|ftp|file)://[-A-Z0-9+&##/%?=~_|$!:,.;]*[A-Z0-9+&##/%=~_|$]/ig, "<a href='$&' target='blank'>$&</a>");
Chrome, for example, complains that & is not valid (as does IE8). Is there some way to escape the ampersand (or whatever else is wrong), without resorting to the RegEx object?
Those testers let you input the regex in its raw form, but when you use it in source code you have to write it in the form of a string literal or (as is the case here) a regex literal. JavaScript uses forward-slashes for its regex-literal delimiters, so you have to escape any slashes in the regex itself to avoid confusing the interpreter.
Once you escape the slashes it should stop complaining about the ampersand. That was most likely caused by the malformed regex literal.
I recognize that regex, having used it myself the other day; you got it from RegexBuddy's Library, didn't you? If you had used RB's "Use" feature to create a JS-compatible regex, it would have escaped the slashes for you.
This works for me in Chrome
var fixed = text.replace(/(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/igm, "<a href='$1' target='blank'>$1</a>");

Categories

Resources