How can I replace Space with %20 in javascript? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery / Javascript replace <space> in anchor link with %20
I am getting sParameter like this :
sParameter = document.getElementById('ddParameterType').value;
If I am getting word like "Test - Text" as the ddParameterType item, then I am replacing the space in word like below:
sParameter = document.getElementById('ddParameterType').value.replace("","%20");
but it is returning a valur like %20Test - Text. I need like Test%20-%20Text.

sParameter = encodeURIComponent(sParameter.trim()) //"Test%20-%20Text"
the .trim will remove leading and trailing whitespace from the string. encodeURIComponent will URL-encode it.

replace replace("","%20"); with replace(/ /g,"%20");
http://www.w3schools.com/jsref/jsref_replace.asp

sParameter = encodeURIComponent(sParameter.trim())

Use the following instead to replace all occurrences:
document.getElementById('ddParameterType').value.replace(/ /g, "%20");
Or better yet:
encodeURIComponent(document.getElementById('ddParameterType').value);

Related

Replace a particular string with an escape character in JavaScript [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 2 years ago.
JavaScript
I have a string as follows:
#UNICODE#{1f600} #UNICODE#{1f600}
and I want to replace each occurrence of '#UNICODE#' with '\u',
so the output should be like
\u{1f600} \u{1f600}
Tried many different regex in .replace function but no luck.
like
('#UNICODE#{1f600} #UNICODE#{1f600}').replace(/#UNICODE#/g,/\u/)
/\u/{1f600}/\u/{1f600}
('#UNICODE#{1f600} #UNICODE#{1f600}').replace(/#UNICODE#/g,\u)
Invalid unicode escape sequence
and so on.
Any bright ideas ?
Thanks in advance.
Escape the \\u
let str = '#UNICODE#{1f600} #UNICODE#{1f600}'.replace(/#UNICODE#/g,"\\u")
console.log(str)
str = str.replace(/\\u\{/g,"&#x").replace(/\}/g,";")
console.log(str)
document.getElementById("x").innerHTML = str
<span id="x"></span>

find and replace '%20' with a space in a string javascript [duplicate]

This question already has answers here:
Javascript replace all "%20" with a space
(7 answers)
Closed 4 years ago.
I'm having some trouble trying to figure this out,
basically I have a url string like so this%20is%20a%20string now what I want to do is find and replace all instances of %20 and replace with a space so the string then becomes this is a string.
Now I've tried to do something like this..
if(string.includes('%20')) {
const arr = str.split('%20');
}
which splits the string into an array, but I'm not sure how I can then turn the array of seperate strings into a full string with spaces between each word.
Any help would be appreciated.
Using regex,
str.replace(/%20/g, ' ');
Just use join:
str.split('%20').join(" ")
let val = "this%20is%20a%20string".replace(/%20/g, ' ');
alert(val);
replace

How to replace a word in JavaScript? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 4 years ago.
Suppose we have a string: "Someone one one-way"
And I want to replace THE WORD "one" with THE WORD "two".
Note: I don't want "someone" or "one-way" to change.
I tried to use str.replace(/one/g , "two");
But the result was "Sometwo two two-way"
This is not the intended result...
I want to get "Someone two one-way"
ANY HELP WOULD BE APPRECIATED. THANKS IN ADVANCE....
You can validate the spaces too:
//var str = "Someone one one-way";
var str = "one one Someone one one-way one,one";
var replace='two';
//using lookahead and lookbehind
var result= str.replace(/(?<=[^a-zA-Z-]|^)one(?=[^a-zA-Z-]|$)/gm , "two");
console.log(result);
//result: two two Someone two one-way two,two
eval('STR.SPLIT(" ONE ").JOIN(" TWO ")'.toLowerCase())
try this regex:
\s(one)\s
you may add modify it for case sensitive.
let input = "Someone one one-way"
output = input.split(" ").map((item)=> item ==='one'? 'two' : item).join(" ");
console.log(output)
You can try this:
.replace(/\bone\b(?!-)/g , "two")
\b matches on a word boundary.
(?!-) is a negative lookahead because you do not want to replace one if followed by a dash. \b recognizes the dash as a word boundary.
There is another pitfall when you have something like "-one".
Use this if this is a Problem:
.replace(/[^-]\bone\b(?!-)/g , function(x){return x.substring(0,1) + 'two'})
Ideally you would use negative look ahead, but this is currently not supported by all Browsers.

javascript - split without losing the separator [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript Split without losing character
I have a string:
"<foo>abcdefg</bar><foo>abcdefg</bar><foo>abcdefg</bar><foo>abcdefg</bar>"
I want to separate all instances of "abcdefg" into an array like this:
["<foo>abcdefg</bar>", "<foo>abcdefg</bar>", "<foo>abcdefg</bar>", "<foo>abcdefg</bar>"];
I try:
var str="<foo>abcdefg</bar><foo>abcdefg</bar><foo>abcdefg</bar><foo>abcdefg</bar>";
var Array_Of_FooBars = str.split("</bar>");
alert(Array_Of_FooBars);
But it returns:
["<foo>abcdefg", "<foo>abcdefg", "<foo>abcdefg", "<foo>abcdefg",]
It is removing the separator ''. I don't want that.
How can I use split and not lose the separators from the string?
Thanks.
Ken
Try this. It's not a perfect solution, but it should work in most cases.
str.split(/(?=<foo>)/)
That is, split it in the position before each opening tag.
EDIT: You could also do it with match(), like so:
str.match(/<foo>.*?<\/bar>/g)
It seems that you would most likely want to use match:
var s = "<foo>abcd1efg</bar><foo>abc2defg</bar><foo>abc3defg</bar><foo>abc4defg</bar>"
s.match(/(<foo>.+?<\/bar>)/g)
// =>["<foo>abcd1efg</bar>", "<foo>abc2defg</bar>", "<foo>abc3defg</bar>", "<foo>abc4defg</bar>"]
You could just iterate over a simple regular expression and build the array that way:
var x = new RegExp('<foo>(.*?)</bar>', 'ig'),
s = "<foo>abcdefg</bar><foo>abcdefg</bar><foo>abcdefg</bar><foo>abcdefg</bar>",
matches = [];
while (i = x.exec(s)) {
matches.push(i[0]);
}
Just realized using String.match() would be better; this code would be more useful for matching the contents inside the tags.
Use positive lookahead so that the regular expression asserts that the special character exists, but does not actually match it:
string.split(/<br \/>(?=&#?[a-zA-Z0-9]+;)/g);

How can I replace '/' in a Javascript string with another character [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript replace all / with \ in a string?
I have a date='13/12/2010' I want to replace this '/' to '_' or something else.
I read this But I do not know how can that applied for my case .
Use a global RegEx (g = global = replace all occurrences) for replace.
date = date.replace(/\//g, '_');
\/ is the escaped form of /. This is required, because otherwise the // will be interpreted as a comment. Have a look at the syntax highlighting:
date = date.replace(///g, '_');
One easiest thing :)
var date='13/12/2010';
alert(date.split("/").join("_")); // alerts 13_12_2010
This method doesn't invoke regular expression engine and most efficient one
You can try escaping the / character like this -
date.replace( /\//g,"_");

Categories

Resources