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.
Related
This question already has an answer here:
Escape string for use in Javascript regex [duplicate]
(1 answer)
Closed 2 years ago.
I've got a very simple script working that filters job opportunities by their name (see list image). This dropdown filters everything, except any opportunity name that has parentheses or a plus sign, and I'm not up to scratch enough to understand how I can effectively get around this issue without changing 102,000 rows in a database.
This is the snippet of code that performs the RegExp match (it's working fine, aside from those characters)
return _.orderBy(
this.results.filter(result =>
result.opportunity.match(
RegExp(this.opportunitySearch, "i")
)
)
);
And as you can see here, this is how Vue is receiving it:
Is anyone able to help point me in the right direction please?
Escape your regex. Those 3 characters need to be escaped or will be interpreted as meta characters.
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
return _.orderBy(
this.results.filter(result =>
result.opportunity.match(
RegExp(escapeRegExp(this.opportunitySearch), "i")
)
)
);
This question already has answers here:
Remove all backslashes in Javascript
(4 answers)
Closed 4 years ago.
I am trying to convert string that has following values
"A\"s\"sets"
my goal is to remove from string \ values no matter how many of them appear in string.
"A"s"sets"
I tried using new RegExp but I do not manage to perform that operation.
I even managed to create regex that will pick up everything except \ sign
[a-zA-Z0-9'"*]
I also tried calling on
regex.exec(string)
but I am getting an array instead of cleared string.
Anyone have any idea how to do this ?
Thank you
You can use replace.
let str = `"A\"s\\"sets"`
let op = str.replace(/\\+/g, '')
console.log(op)
This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 5 years ago.
Is there any way to make something.+()[]* matching literally 'something.+()[]*'? I'm using regex builder so manual escaping is not allowed. Sure, i can add hardcoded checks if (char === '+') return '\+' but i'm looking for native solution or better way
UPD
I'm sorry. I forgot to add that matching should be in given order with moving forward but not back. So [+.] will not fit my requirements because it will match both +. and .+. I need only first case (In definition order)
You don't need to escape them if within square brackets.. I just tested and works for me, but maybe not what you are looking for?
something[.+()[]]
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:
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