我有一个JS字符串需要替换它的几个字符.
例如,对于输入字符串:
s = 'ABAC'
Run Code Online (Sandbox Code Playgroud)
我想B用Cs 替换所有s,反之亦然.但是,执行标准正则表达式替换是不够好的,因为replace()s不应该以锁步方式发生,而是在字符串上单次传递.
>>> s.replace(/B/g, 'C').replace(/C/g, 'B')
'ABAB' // not good
Run Code Online (Sandbox Code Playgroud)
是否有一种优雅的方式replace()在一次传递中做多个字符串?
(解决方案必须适用于任意替换char)
我们必须为我们的老师做一个小程序,以获取Javascript中任何值的ASCII代码.
我搜索和研究过,但似乎没有方法可以这样做.我发现:
charCodeAt()
返回Unicode值,但不返回ASCII.
我在这个论坛中读到,ASCII值与已经具有ASCII值的ASCII字符的Unicode值相同:
但似乎并非总是如此,例如扩展的ASCII字符.例如:
var myCaracter = "?";
var n = myCaracter.charCodeAt(0);
document.write (n);
Run Code Online (Sandbox Code Playgroud)
该字符的ASCII值为195,但程序返回226(Unicode值).
我无法找到要从一个转换为另一个的模式,因此:
¿我们可以从Unicode获取ASCII,还是应该寻找其他方式?
谢谢!
我试图摆脱一些特殊字符。
\n\n在本例中,瑞典语 \xc3\x85\xc3\x84\xc3\x96 和 \xc3\xa5\xc3\xa4\xc3\xb6。为什么这不起作用?
\n\n结果应该是,AAOaao但我得到了\xc3\x85\xc3\x84\xc3\x96\xc3\xa5\xc3\xa4O?
$stringX = "\xc3\x85\xc3\x84\xc3\x96\xc3\xa5\xc3\xa4\xc3\xb6"\n\n$stringX = ($stringX -replace "\xc3\x85$","A")\n$stringX = ($stringX -replace "\xc3\x84$","A")\n$stringX = ($stringX -replace "\xc3\x96$","O")\n$stringX = ($stringX -replace "\xc3\xa5$","a")\n$stringX = ($stringX -replace "\xc3\xa4$","a")\n$stringX = ($stringX -replace "\xc3\xb6$","o")\n\n"Result = $stringX"\nRun Code Online (Sandbox Code Playgroud)\n 如何将unicode字符串转换为ascii为友好的URL创建一个很好的字符串?
我想添加一个像这个小提琴的元素,但是当字符串具有UTF-8字符时,我有一个问题来比较我的字符串,因为"é" is > at all basics letters.
<ol class="ingredientList">
<li class="ingredient">Apples</li>
<li class="ingredient">Carrots</li>
<li class="ingredient">Clams</li>
<li class="ingredient">Oysters</li>
<li class="ingredient">Wheat</li>
</ol>
<ol class="ingredientList">
<li class="ingredient">Barley</li>
<li class="ingredient">éggs</li>
<li class="ingredient">Millet</li>
<li class="ingredient">Oranges</li>
<li class="ingredient">Olives</li>
</ol>
Run Code Online (Sandbox Code Playgroud)
$(".ingredient").click(function() {
var element = $(this);
var added = false;
var targetList = $(this).parent().siblings(".ingredientList")[0];
$(this).fadeOut("fast", function() {
$(".ingredient", targetList).each(function() {
if ($(this).text() > $(element).text()) {
$(element).insertBefore($(this)).fadeIn("fast");
added = true;
return false;
}
});
if (!added) $(element).appendTo($(targetList)).fadeIn("fast");
});
});
Run Code Online (Sandbox Code Playgroud)
你有解决方案吗?