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

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

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>

How do I insert something at a specific character with Regex in Javascript [duplicate]

This question already has answers here:
Simple javascript find and replace
(6 answers)
Closed 5 years ago.
I have string "foo?bar" and I want to insert "baz" at the ?. This ? may not always be at the 3 index, so I always want to insert something string at this ? char to get "foo?bazbar"
The String.protype.replace method is perfect for this.
Example
let result = "foo?bar".replace(/\?/, '?baz');
alert(result);
I have used a RegEx in this example as requested, although you could do it without RegEx too.
Additional notes.
If you expect the string "foo?bar?boo" to result in "foo?bazbar?boo" the above code works as-is
If you expect the string "foo?bar?boo" to result in "foo?bazbar?bazboo" you can change the call to .replace(/\?/g, '?baz')
You don't need a regular expression, since you're not matching a pattern, just ordinary string replacement.
string = 'foo?bar';
newString = string.replace('?', '?baz');
console.log(newString);

how to split String based on \r\n [duplicate]

This question already has answers here:
How to split newline
(13 answers)
Closed 7 years ago.
We are Developing phonegap application.We get Data form CSV file. It's look like this
We need Data Split into two strings Like
String1
String2
We tried like this but We don't have luck so Please guide me
var split = string.split('\r\n');
Please help me
try this:
var split = string.split(/\n/);
Replace the new line characters with space and then split the string with space
string.replace( /\n/g, " " ).split(" ");
UPDATE:
var string1=string.substring(0,string.indexOf("TOTALAMOUNT"));
var string2=string.substring(string.indexOf("TOTALAMOUNT"),string.length);
Or if your string contains \n then:
var string1=string.substring(0,string.indexOf("\n"));
var string2=string.substring(string.indexOf("\n"),string.length);
alert(string1);
alert(string
Fiddle

Replace substring with edited substring [duplicate]

This question already has answers here:
Why isn't this split in javascript working?
(2 answers)
Closed 8 years ago.
please, could you help me with my task: I need to replace part of string and probably the best way is regular expression but I don't know, how to make it working. I want to do this:
http://someweb.com/section/&limit=10&page=2
replace page=2 with page=3 so string will be:
http://someweb.com/section/&limit=10&page=3
I tried to do something like this:
// set string in t variable
t.replace('/page=[0-9]/', 'page=$1++') });
Thank you very much for your help :)
In our case first argument should be regexp, but in your variant this is string '/page=[0-9]/' (remove '). In replace you can pass function as second argument, and do with matched data what you want. (for example add +1 to page=)
var str = "http://someweb.com/section/&limit=10&page=2";
str.replace(/page=(\d+)/, function (match, page) {
return 'page=' + (+page + 1); // plus before page converts string to number
});
Example
You can also try below code.
var url = "http://someweb.com/section/&limit=10&page=2",
reExp = /page=([0-9])+/,
result = reExp.exec(url);
url = url.replace(reExp, 'page=' + (+result[1] + 1));
console.log(url)

Regular Expression only returning first result found [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?
I am trying to parse an xml document like this:
var str = data.match("<string>" + "(.*?)" + "</string>");
console.log(str);
I want to get all the elements between the [string] in an array but for some reason, it only returns the first string element found. Im not good with regular expressions so Im thinking this is just a small regex issue.
You want it to be global g
var str="<string>1</string><string>2</string><string>3</string>";
var n=str.match(/<string>(.*?)<\/string>/g);
//1,2,3
You have to form the RegEx adding a g to it like
/Regex/g

Categories

Resources