Can't replace UTF-8 character with RegExp - javascript

I have a string with the UTF-8 character ↵. To my understanding, if you want to replace a UTF-8 character in a string, you specify the character with its hexadecimal representation, like so:
var string = "↵↵↵Middle↵↵↵";
console.log("Match? " + /\u21b5/.test("↵"));
console.log(string);
string = string.replace("/\u21b5/g", "");
console.log(string);
It is a match, but the replace is not working. What am I missing?
JSFiddle

You are using a string not a regex
string = string.replace(/\u21b5/g, "");

replace
string = string.replace("/\u21b5/", "");
with
string = string.replace(/\u21b5/g, "");

Related

Is it possible to do a double replace?

If double replace is it possible to do?
var string = [link="<iframe"qwe"></iframe>"]
var output = string.replace(/[link="([^"]+)"]/g, '$1.replace(/"([^"]+)"/g, "'")');
what i want output:
[link="<iframe'qwe'></iframe>"]
You can use a function as the replacement in replace(). It can then do its own replace on the capture group.
var string = '[link="<iframe"qwe"></iframe>"]';
var output = string.replace(/link="([^\]]+)"]/g, (match, group1) =>
'link="' + group1.replace(/"/g, "'") + '"]');
console.log(output);
Note also that I had to correct your regexp. ([^"]+) should be ([^\]]+) so that it can match a string containing double quotes -- you need to capture that so you can replace the double quotes.
And in the second replacement, you want to match ", not [^"]+

Remove all special characters from string in JS

I want to remove all special characters from string and add only one "-"(hyphen) in the place.
Consider below example
var string = 'Lorem%^$&*&^Ipsum#^is#!^&simply!dummy text.'
So, from the above string, if there is a continuous number of special characters then I want to remove all of them and add only one "-" or if there is a single or double special character then also that should be replaced by "-"
Result should be like this
Lorem-Ipsum-is-simply-dummy text-
I have tried below, but no luck
var newString = sourceString.replace(/[\. ,:-]+/g, "-");
You could use .replace to replace all non-alphabetical character substrings with -:
const input = 'Lorem%^$&*&^Ipsum#^is#!^&simply!dummy text.';
const output = input.replace(/[^\w\s]+/gi, '-');
console.log(output);
If you want to permit numbers too:
const input = 'Lorem123%^$&*&^654Ipsum#^is#!^&simply!dummy text.';
const output = input.replace(/[^\w\s\d]+/gi, '-');
console.log(output);

javascript string replace ( with \(

How can I transform the string "Test(5)" into "Test\(5\)" dynamically? (JQuery or Javascript)
I have tried this but with no success
var string = "Test(5)";
string = string.replace("(","\(");
string = string.replace(")","\)");
console.log(string);
http://jsfiddle.net/mvehkkfe/
I assume you meant
replace the string "Test(5)" into "Test\(5\)"
In which case:
var string = "Test(5)";
string = string.replace("(","\\(");
string = string.replace(")","\\)");
console.log(string);
Escape the backslash
If you want to replace the string "Test(5)" into "Test\(5\)", you can use below code
var string = "Test(5)";
string = string.replace("(","\\(");
string = string.replace(")","\\)");
console.log(string);

How do I convert unicode from an HTML input to a greek character in javascript?

How do I convert unicode from an HTML input to a greek character in javascript? The first example does not work, but the second does.
var str = input.value; \u03B1 (typed into input box)
console.log(str); \u03B1
var str = "\u03B1"; assigned directly
console.log(str); α
To convert unicode literals in strings to actual characters you can just run them though String.prototype.replace with String.fromCharCode
var str = '\\u03B1\\u03B2\\u03B3\\u03B4'; // "\u03B1\u03B2\u03B3\u03B4"
str.replace(/\\u([\da-fA-F]{4})/g, function (m, $1) {
return String.fromCharCode(parseInt($1, 16));
}); // "αβγδ"
The backslash is escaping in the second variable str - in the first value, if input.value was \u03B1, it would ACTUALLY be the same as var str = "\\U03B1" to invalidate the backslash by escaping it.
If you want to evaluate the escaped character in the field, you can do so like this:
var str = input.value.replace("\\u", "");
str = String.fromCharCode(parseInt(str, 16));
This works because you are parsing an integer from everything after \u and passing that into fromCharCode. Character codes are in integers - you are parsing that code from the original \u23B1 code.

How to strip last element of dash delimited string in Javascript

I have string delimited with dashes like:
x#-ls-foobar-takemeoff-
How can I remove takemeoff- using javascript where takemeoff- can be any amount of characters ending in a dash?
var str = "x#-ls-foobar-takemeoff-";
var newStr = str.replace(/[^-]+-$/,"");
Basic regular expression says
[^-]+ <-- Match any characters that is not a dash
- <-- Match a dash character
$ <-- Match the end of a string
If you have a string str, you can do the following:
str = str.substr(0, str.lastIndexOf("-", str.length - 2));
Using substr() and lastIndexOf():
var myStr = "x#-ls-foobar-takemeoff-";
myStr = myStr.substr(0, myStr.length-1); // remove the trailing -
var lastDash = myStr.lastIndexOf('-'); // find the last -
myStr = myStr.substr(0, lastDash);
alert(myStr);
Outputs:
x#-ls-foobar
jsFiddle here.

Categories

Resources