Replace Single Quotes in Javascript String Support UTF-8 - javascript

In my web application I get data from Instagram. Some full names have single quotes like:
♠ فال ورق ' تاروت ' پیشگویی
I'm trying to remove or replace them with an empty character, this code doesn't work and I can't replace them:
.replace(/("|')/g, "")
or
.replace(/["']/g, "")
How can I change this code to remove the single quotes?

How about this solution? Hope it helps!
console.log("فال ورق ' تاروت ' پیشگویی".replace(/'/g, ""));
console.log("code'123".replace(/'/g, ""));

Related

How to render unicode escape sequences using javascript?

Below code,
document.writeln("Caracteres escapados {\u55e8\uff0c\u4f60\u597d\u5417}: "
+ "\u55e8\uff0c\u4f60\u597d\u5417");
expected output is \u55e8\uff0c\u4f60\u597d\u5417: 嗨,你好吗
Current output is: Caracteres escapados {嗨,你好吗}: 嗨,你好吗
What modification is required in the above code?
You need to escape your backslashes and remove the braces.
document.writeln("Caracteres escapados \\u55e8\\uff0c\\u4f60\\u597d\\u5417 "
+ "\u55e8\uff0c\u4f60\u597d\u5417");
Just escape the backslashes of the codes you want to show without translating:
document.writeln("Carácteres escapados \\u55e8\\uff0c\\u4f60\\u597d\\u5417: \u55e8\uff0c\u4f60\u597d\u5417");

How to remove ' in java script

I have problem in removing ' with (blank and no space). Like Kello's to kellos.
I already tried this-
str = str.replace(/[\']/g, '');
But its not working.
Please help.
It actually does work:
var str = 'aa\'bb\'cc';
alert(str.replace(/'/g,'')); // aabbcc
alert(str.replace(/[\']/g,'')); // aabbcc
You do not need a character class, you just have to mask it if you use single quotes in JavaScript.
Also, keep in mind that ' (U+0027) is different from ’ (U+2019) and must be handled separately.
A Kello's
str.replace(/[{\ },{\'}]/g,"");
result AKellos

Regular expression for removing whitespaces

I have some text which looks like this -
" tushar is a good boy "
Using javascript I want to remove all the extra white spaces in a string.
The resultant string should have no multiple white spaces instead have only one. Moreover the starting and the end should not have any white spaces at all. So my final output should look like this -
"tushar is a good boy"
I am using the following code at the moment-
str.replace(/(\s\s\s*)/g, ' ')
This obviously fails because it doesn't take care of the white spaces in the beginning and end of the string.
This can be done in a single String#replace call:
var repl = str.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
// gives: "tushar is a good boy"
This works nicely:
function normalizeWS(s) {
s = s.match(/\S+/g);
return s ? s.join(' ') : '';
}
trims leading whitespace
trims trailing whitespace
normalizes tabs, newlines, and multiple spaces to a single regular space
Try this:
str.replace(/\s+/g, ' ').trim()
If you don't have trim add this.
Trim string in JavaScript?
Since everyone is complaining about .trim(), you can use the following:
str.replace(/\s+/g,' ' ).replace(/^\s/,'').replace(/\s$/,'');
JSFiddle
This regex may be useful to remove the whitespaces
/^\s+|\s+$/g
Try:
str.replace(/^\s+|\s+$/, '')
.replace(/\s+/, ' ');
try
var str = " tushar is a good boy ";
str = str.replace(/^\s+|\s+$/g,'').replace(/(\s\s\s*)/g, ' ');
first replace is delete leading and trailing spaces of a string.

How to replace all occurring same character like ' with ;$39

I am using following code, in which i want to replace all ' with ;$39 but its not working fine . It's replace only first ' .
var searchUserName = document.getElementById("ctl00_ContentMain_UserSearchColl").value.replace("/\'/g", ";$39;");
For example: Ram's's .Output: Ram;$39s;$39s
Thanks in advance.
You don't need to put the regexp inside double quotes. Remove them.
value.replace(/'/g, ';$39;')
Also note that you need not "escape" the single quote. (Thanks #Paul S. for pointing)

JavaScript Replace - u0009 .... with .replace(/\u0009/g,'');

I'd like to use Javascript to replace all instances of \u009 in a string
This doesn't seem to be working: .replace(/\u0009/g,'');
Do I need to escape something?
First, the question says "replace all instances of \u009 in a string".
But, the regex has replace(/\u0009/g,''); Is this a typo (different number of zeroes)?
Anyway, if the string only contains, unicode, horizontal tab characters (just one char), then the regex is fine.
If it actually contains 6 ascii chars, then the regex needs to be escaped, like so:
var oneChar = 'Pre \u0009 post';
var sixChars = 'Pre \\u0009 post';
//-- NOTE: If not using Firebug, replace 'console.log()' with 'alert()'.
console.log (oneChar + ' becomes --> ' + oneChar.replace (/\u0009/g, "") );
console.log (sixChars + ' becomes --> ' + sixChars.replace (/\\u0009/g, "") );
You need another escape .replace(/\\u009/g,''); :)

Categories

Resources