Replace parentheses in URL strings [duplicate] - javascript

This question already has answers here:
Passing "(" and ")" through a URI causes a 403 error, how can I encode them?
(2 answers)
Closed 1 year ago.
I have string with URL links like below. But urls are breaking when urls have parenthesis like below. The urls are breaking at the start of parenthesis in URL.
This is test http://ang.wikipedia.org/wiki/Wikipedia:Tutorial_(Wikipedia_links)
Can we replace parentheses with other ASCII characters using javascript regex?

Use the string.replace() method.
url = url.replace(/\(/g, '%28').replace(/\)/g, '%29');

Related

can '?' separator be included twice in the URL [duplicate]

This question already has answers here:
Is it valid to have more than one question mark in a URL?
(2 answers)
Closed 1 year ago.
https://www.websitename.com/?audience=testingting?internal-abhishek-jwt=random_string
Is this a valid URL? i.e is it fine to have two '?' separators in the URL?
Note: Due to limitations, I can not add '&' to internal-abhishek-jwt=random_string
It isn't. You have to URL encode the second one if you want to use it as a parameter or use '&' symbol instead if you wanna separate your arguments (which seems to be the case here).
Some further explanation here
The ? still indicates the start of the query string.
Segments of the query string are separated by & characters.
This will be parsed as:
key: audience
value: testingting?internal-abhishek-jwt=random_string

req.url.replace(/\/?(?:\?.*)?$/, '') node js [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
Meaning of javascript text between two slashes
(3 answers)
Closed 2 years ago.
I'm referring to a book and it has code I can't understand:
http.createServer(function(req,res){
// normalize url by removing querystring, optional
// trailing slash, and making lowercase
var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
}
I have a problem in the following line:
var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
What is that first argument of replace method?
The first argument is the pattern, a regex on what to look for, and the second argument is to what to replace the instances with. In this case, \/? is the / character with zero or one instance of it, and (?:\?.*)? not to capture the piece where it has ? zero to unlimited times.

whatsapp://send?text cut string from '&' character till end? [duplicate]

This question already has answers here:
Escaping ampersand in URL
(8 answers)
Closed 6 years ago.
I'm trying to add a link that can be shared in WhatsApp:
For example:
"whatsapp://send?text=http://www.example.com/products/women/dresses?sessionid=34567&source=google.com"
But the link that is sent in WhatsApp is truncated from the '&' until the end. (the second parameter)
The same thing happens when I try:
"WhatsApp://send?text=http://www.example.com/prod&ucts/"
(note the '&' in the middle of the word 'prod&ucts')
The text after the '&' is truncated.
Any suggestions why this is happening and how can I fix it?
It'll be cutting it off because an '&' denotes a new query string parameter. You can fix it by encoding the string parameter in your URL using the encodeURI JavaScript function. The & character will encode to "%26".

Replace string including asterisk in Javascript [duplicate]

This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 7 years ago.
I am trying to replace the same string *a*a consistently with *a.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a being replaced with xyz
* is a special regex character. If you want to match only an actual asterisk, then you have to escape it like this:
s = s.replace( /\*a\*a/g, "*a");
Working demo: http://jsfiddle.net/jfriend00/gvgshwyz/
An asterisk is a special regex character.
You just have to escape it like this: \*a in place of *a

Parsing JSON containing new line characters [duplicate]

This question already has answers here:
How do I handle newlines in JSON?
(10 answers)
Closed 8 years ago.
In my website I try to convert a string to JSON which contains a newline.
JSON.parse('{"hallo":"line1\r\nline2","a":[5.5,5.6,5.7]}');
This produces an "Unexpected token" error. Do I need to escape that somehow?
Yes, you should escape both \n and \r as they belong to the list of control characters. Full list of characters that need to be escaped can be found here. Your code would be
obj = JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');
JSFiddle: link
Try:
JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');

Categories