如何解决js字符串中这个从右到左的脚本问题? [英] how to fix this right-to-left script issue in js string?
问题描述
当使用奇怪的字符串时,我想要默认行为而不是这个从右到左的脚本问题.
I would like the default behavior instead of this right-to-left script issue when using the strange string.
我什至无法在问题中输入预期的输出.浏览器会自动把它放在错误的一边.
var strange = "د.إ";
var common = "$";
var amount = 50;
var str_common = common + amount;
var str_strange = strange + amount;
alert(str_common);
// unexpected output
alert(str_strange);
// unexpected output
alert(str_common + ' -- ' + str_strange);
// unexpected output
alert(str_strange + ' -- ' + str_common);
js 小提琴 - http://jsfiddle.net/rahulroy9202/xL7cu2os/1/
推荐答案
你可以使用 unicode 控制字符 U+202A
来强制它 LTR:
You could make use of the unicode control character U+202A
to force it LTR:
var str_strange = '\u202A' + strange + amount;
\u202A
是 LTR 嵌入的 unicode 控制字符.\u202B
用于 RTL 嵌入.
\u202A
is the unicode control character for LTR embedding. \u202B
is for RTL embedding.
更多信息在这里:https://en.wikipedia.org/wiki/Bi-directional_text#Table_of_possible_BiDi-types
您也可以使用 \u200..
系列.只需使用 \u200E
将其强制为 LTR.
You could also use the \u200..
series. Just use \u200E
for forcing it to LTR.
代码段(带组合):
var strange = "د.إ", common = "$", amount = 50, strStrange = '';
strStrange = '\u202A' + strange + amount;
alert(strStrange);
strStrange = '\u200E' + strange + '\u200E' + amount;
alert(strStrange);
这篇关于如何解决js字符串中这个从右到左的脚本问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!