我想在textarea上设置一个计数器来计算两次特殊字符,并在击键后一次计算其他字符。
这是我到目前为止所做的代码:
from bidi.algorithm import get_display
import PIL.Image, PIL.ImageFont, PIL.ImageDraw
import arabic_reshaper
unicode_text = u"\u06F1"+ u"\u06CC"+ u"\u06A9"
list_of_letters = list (unicode_text)
folder = 1
n=2
for i in range(1,n+1):
for word in itertools.permutations( list_of_letters,i):
print word
t1 = arabic_reshaper.reshape(word)
print t1
img= PIL.Image.new("L", (200, 200))
draw = PIL.ImageDraw.Draw(img)
font = PIL.ImageFont.truetype( r"C:\Users\arabtype.ttf", 40)
t2 = get_display(t1)
print t2# <--- here's the magic <---
draw.text( (10,50), ' ' + t2, fill=220, font=font)
img.show()
&#13;
function count_up(obj) {
var count = obj.value.length;
document.getElementById('count1').innerHTML = count;
}
&#13;
答案 0 :(得分:1)
您需要使用非特殊字符数来计算特殊字符的数量。
这样的事情:
function count_up(obj) {
//Matches characters that are not A-Z or 0-9
var countSpecial = (obj.value.match(/[^a-zA-Z0-9]/g) || [] ).length * 2;
//Matches characters that are A-Z or 0-9 (because of the ^ symbol)
var count = (obj.value.match(/[a-zA-Z0-9]/g) || [] ).length;
document.getElementById('count1').innerHTML = count + countSpecial;
}
&#13;
<form>
<div class="form-group">
<label>Count Up</label>
<textarea class="form-control" rows="5" onkeyup="count_up(this);" maxlength="20"></textarea>
<span class="text-muted pull-right" id="count1">0</span>
</div>
<br><br>
</form>
&#13;
答案 1 :(得分:0)
或者,您可以简单地计算字母数字,并从总数中减去 - 它从未真正规定您需要实际的字符串,只需计数。
document.querySelector("#count-this").addEventListener("keyup", count_up);
function count_up(evt) {
var obj = evt.target;
var text = obj.value;
var alphaNumeric = text.replace(/[\W_]+/gmi, "");
var specialCount = text.length - alphaNumeric.length;
var count = alphaNumeric.length;
// console.log(text + ", " + alphaNumeric);
document.querySelector('.alphanumeric-count .counter').innerHTML = count;
document.querySelector('.non-alphanumeric-count .counter').innerHTML = specialCount;
}
<form>
<div class="form-group">
<label>Count Up</label>
<textarea class="form-control" rows="5" id="count-this" maxlength="20"></textarea>
</div>
<div class="results">
<div class="alphanumeric-count">
Alphanumeric: <span class="text-muted pull-right counter" >0</span>
</div>
<div class="non-alphanumeric-count">
Special: <span class="text-muted pull-right counter" >0</span>
</div>
</div>
</form>
如果您愿意,可以 as a fiddle 。