This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I decode a URL with jQuery?
Could somebody please help me to take a string (i.e. URL parameter) and remove the +(plus) or %20 to a regular space (" ")?
For example, to change this:
"my+string+to+change" or "my%20string%20to%20change"
to this:
"my string to change"
I use a lot of jQuery already, so that would be fine. Or pure javascript if that's easier.
Use decodeURIcomponent(stringToDecode)
Note this is not a jquery feature but a regular javascript function.
Related
This question already has answers here:
Parse an HTML string with JS
(15 answers)
Cross-Browser Javascript XML Parsing
(3 answers)
Parse XML using JavaScript [duplicate]
(2 answers)
Trying to use the DOMParser with node js
(8 answers)
Closed 2 years ago.
Is there a feature in javascript such as deleting words starting with "<" and ending with ">"?
For example I want to extract html codes from this object
"description": "<mainText><stats><attention> 25</attention> Hareket Hızı</stats></mainText><br>",
Sounds like a job for regex.
I think this could do it:
// BAD CODE
function useRegex(input) {
let regex = /^<[a-zA-Z]+> <[a-zA-Z]+><[a-zA-Z]+>$/i;
return regex.test(input);
}
EDIT: seems like my idiot self has messed this one up. Don't use regex, as commented by someone else.
I personally hate regex, so here are some tools to help you
Website to help you get started
https://regexr.com/
And how to apply that in Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Regex code creator. It's like magic
https://regex-generator.olafneumann.org/
This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 2 years ago.
Please note that this is not a json object :) My string is:
{"message":"***error in SAP module:-1***","status":400}
This is not a json object, this is a pure string. I cannot turn it into a json object due to technical limitations.
So, I want to take only the bold string (all the value of "message").
I thought about lastIndexOf, and pick the string between ":" and ","
But I got messed up with the escape characters for the quotes.
How can I achieve it with lastIndexOf? Or in another better way?
If you can't use JSON.parse, I would use a regex to read it. You know you want the string directly following "message":", so I would look for that, then grab everything from there to the next occurrence of "
This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 6 years ago.
I am communicating to a REST server which is expecting something like http://example.com/1.0/editor/5/bla/26 along with body parameters of x, y, and z. Or maybe instead of /1.0/editor/5/bla/26, it should be 1.0/editor/5/bla/what about item #26, but of course with what about item #26 escaped.
There are many post describing how to use jQuery.param() to encode parameters, however, that is not my question. How chould the actual url be created using jQuery of JavaScript?
$.ajax({
type:'PUT',
url:'/1.0/editor/'+$('#id').val()+'/bla/'+$('#wha').data('id'),
data:{x:x,y:y,z:z},
success: function (error){}
});
Can you write a javascript RegEx function to replace all spaces with a known character? Or replace it with its HTML equivalent. Then in your backend, just look for that escape character and replace them with the spaces again. It's kind of a work around but depending on your situation it could work.
This question already has answers here:
remove script tag from HTML content
(13 answers)
Closed 7 years ago.
I want to delete all javascript found on a string with PHP, I'm trying to do it trough preg_replace like this: $text = preg_replace("/<script.*<\/script>/mis", "", $text);
But after doing this, $text is an empty string. What I'm doing wrong? Trying this regex on http://www.phpliveregex.com/ it seems to work, but using it in real life returns me nothing.
Typically you do not just want to remove (escape) javascript but all sorts of HTML tags which prevent a vulnerability to you. You can do so using
http://docs.php.net/manual/en/function.htmlspecialchars.php
or
http://docs.php.net/manual/en/function.htmlentities.php
Then you dont have to bother with regex altogether.
This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 8 years ago.
how do i replace the asterisks with a blank ("") in the innerhtml using javascript?
i have tried this.
document.getElementById("lab").innerHTML = document.getElementById("lab").innerHTML.replace(/*/g, '');
and i also tried doing the asterisk itself but it gets commented..
searched everywhere i can't seem to find ways to remove the appended in innerhtml aside from replacing it with a blank.. are there any other options?
i also tried searching for ways to use replace and other options. for added info i am using this for the validation of a form
thanks in advance!
You have to escape the asterisk:
.....replace(/\*/g, "")
because it is a special character in regular expressions.