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

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>

Related

Extract text from enclosing parentheses using JavaScript [duplicate]

This question already has answers here:
Get text between two rounded brackets
(7 answers)
Closed 2 years ago.
I have a string like Manila (Philippines) and want to replace it with only the substring Philippines. I tried using the following regex pattern, which works in Notepad++:
[^\(]+ \(([^\)]+)\)
However, I get an undefined result in JavaScript:
var x = "Manila (Philippines)";
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,$1));
You just forgot the " around your replace pattern!
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,"$1")); will work correctly!
You can use .match():
var x = "Manila (Philippines)";
var result = x.match(/\((.+)\)/).pop();
// regex for string contained in parentheses
console.log(result);

String starting with backslash hacks my regex [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 3 years ago.
My string should be in the IRC command format : "/add john".
So, i created this Regex :
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test('\/add user1');
alert(bool);
The problem is either I use /***/ or RegExp syntax, if I set a backslash at the beginning of my string (like in my example above), my alert pop up show "true" and I don't want that.
I code in Javascript
You can use String.raw to make sure that the backlash is not removed when testing your input:
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test(String.raw`\/add user1`);
alert(bool);
You can play with this code here: https://jsbin.com/ziqecux/25/edit?js

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 the backward slash from string? [duplicate]

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
Closed 4 years ago.
What is wrong with the following code?
Expected output : substr1#substr2#substr3
var str = "substr1\substr2\substr3"
// it works if I use the double slash "\\" in thestring but not with single.
console.log(str.replace(/\\/g, "#"));
Your initial string itself do not have a backslash. To verify check the snippet below:
var str = "substr1\sustr2\substr3"
console.log(str);
The actual output you expect can be obtain by first escaping the backslash and then replacing it with #:
var str = "substr1\\sustr2\\substr3"
console.log(str.replace(/\\/g, "#"));

JavaScript how to strip/remove a char from string [duplicate]

This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'

Categories

Resources