This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 8 years ago.
I need to pass some parameters in the url and they can have special characters like ", spanish Ñ or ñ, : spaces and accents.
What is the propper way to encode them before adding to the url or in case I got in the html like that, read them?
I tried this:
arrayData[i] = pair[1].replace('+', " ").replace('%22', "\"");
But just get working with + or spaces, not both at the same time or in 2 lines:
arrayData[i] = pair[1].replace('+', " ");
arrayData[i] = pair[i].replace('%22', "\"");
You can try encodeUri Built-in function, for example
encodeURI('coño funcionó!')
Previous answer is correct. JavaScript has built in functions for fulfilling this kind of tasks.
You can try to investigate these functions in w3schools.com. Here are the links with basic information and live "Try it out" feature:
encodeURI - takes string with your characters and encodes it into plausible for url style ( encoding spaces and non ANSII chars )
decodeURI - takes encoded string and decodes it to initial state
Related
This question already has answers here:
AJAX POST and Plus Sign ( + ) -- How to Encode?
(6 answers)
Closed 5 years ago.
I have a web app where if someone selects something in the dropdown menu, it changes the next field with Ajax. I'm having difficulty when the values of the dropdown have a '+' symbol which breaks it.
For example this works:
if ($_GET['ch'] == 'Something here - here') {}
However this does not
if ($_GET['ch'] == 'Something here + here') {}
I'd like a solution to be able to include the + symbol inside. Some symbols seem to work fine including brackets (), dashes -, etc.
Try encodeURI function, and/or use POST instead.
Also escaping characters would be good. (like \+ instead of +)
When you are escaping characters than at php side you should use stripslashes function if you need special characters.
This question already has answers here:
How to get the file name from a full path using JavaScript?
(21 answers)
Closed 6 years ago.
I need a regex that would replace something like this but leave the file name.
/folder1/folder2/folder3/anything/somefile.html
Also could someone show me how to implement this with replace method? Replacing the entire path match to empty string and again leaving the file and which would be anything.
Thanks in advance.
You can do it without regular expressions:
var filename = string.split('/').pop();
// "somefile.html"
You can use .*\/.
. will match anything
* will repeat the previous zero or more times.
\/ is a literal slash (/). But needs to be escaped because it's part of the regex construct:
var str = '/folder1/folder2/folder3/anything/somefile.html';
str.replace(/.*\//, ''); // "somefile.html"
This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 7 years ago.
I have a query string that I need to remove a certain parameter from. For instance, my query string may be "?name=John&page=12&mfgid=320", and I need to remove the "page" parameter from it and end up with "?name=John&mfgid=320". I cannot assume that the "page" parameter is or isn't followed by other parameters.
All my attempts at using JavaScript functions/regex are failing miserably, so I could really use a hand in getting this working. Thanks.
That's quite easy... It's just /page=\d+&?/
var uri = '?name=John&page=12&mfgid=320';
uri = uri.replace(/page=\d+&?/,'');
You can use:
uri = uri.replace(/[?&]page=[^&\n]+$|([&?])page=[^&\n]+&/g, '$1');
RegEx Demo
We'll need to use alternation to cover all the cases of presence of query parameter. Check my demo for all test cases.
This question already has answers here:
How can I send the "&" (ampersand) character via AJAX?
(8 answers)
Closed 7 years ago.
This has been asked before but mine is different. I cant rub my head around this. I have a wysiwyg form that is being fetched by jquery and saved by ajax
var content=$('.inputfield').val();
fields='front='+fcarddetails;
$.ajax({
method:'POST',
url:actionpage,
data:fields,
beforeSend:function()
{
$("#processing").show();
},
complete:function ()
{
$("#processing").hide();
},
success: function(feedback)
{
} etc.
When '&' is added to the field, the whole input is messed up.
I a have handled all the html escapes, filters and special characters. But the code gets broken even before it reaches php action page. I cant convert '&' to & because it still contains '&'. Please help, problem is in js, not php but you can prove me otherwise. Thanks in advance.
Encode the string using encodeURIComponent.
The encodeURIComponent() method encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).
var fcarddetails = 'Tom&Jerry';
fields = 'front=' + encodeURIComponent(fcarddetails);
document.write(fields);
This question already has answers here:
Regular expression for URL
(10 answers)
Closed 8 years ago.
here is my regexp
var url_reg = /^(http[s]?:\/\/|ftp:\/\/)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|in|au)+/;
it works fine for single input like https://www.google.com , but
it allows double or more "http/https/www" like below -
https://www.google.com/https://www.google.com/
url can also include folder like google.com/folder/file
i need to validate single occurrence of valid url.
Can anyone help me?
To validate a URL, you can use a regex. This is what I use. A valid URL per the URL spec. The URL you have provided, is actually a valid URL per the URL spec.
/^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:#&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$/
This was borrowed from OSWAP