gpt4 book ai didi

jquery - 使用正则表达式和 jQuery 选择阿拉伯文本中的模式

转载 作者:行者123 更新时间:2023-12-01 01:16:50 25 4
gpt4 key购买 nike

这也许是一个棘手的问题。使用正则表达式,我想选择括号之间的阿拉伯数字(可以是 1、2 或 3 个阿拉伯数字)以及括号本身:http://jsfiddle.net/QL4Kr/ .

<span>
    كهيعص ﴿١﴾
</span>
<span>
    وَإِلَيْهِ تُرْ‌جَعُونَ ﴿٢٤٥﴾
</span>​
jQuery(function () {    
var oldHtml = jQuery('span').html();
var newHtml = oldHtml.replace("","");
jQuery('span').html(newHtml); });​

最佳答案

这是我想到的:

jQuery(function() {

/* Arabic digits representation in Unicode
See: http://stackoverflow.com/a/1676590/114029 */
var myregex = /[\u0660-\u0669]+/;

$('span').each(function(index) {

var text = $(this).text();

var matches = text.match(myregex);

alert(index + ': ' + $(this).text());

alert(matches);

});

});

这是使用它的 jsFiddle:http://jsfiddle.net/leniel/YyAXP/

<小时/>

这是修改后的版本,仅保留每个 <span> 内的数字。 :

jQuery(function() {

var myregex = /[\u0660-\u0669]+/;

$('span').each(function(index) {

var text = $(this).text();

$(this).text('');

var matches = text.match(myregex);

$(this).text(matches);

});

});

jsFiddle:http://jsfiddle.net/leniel/YyAXP/1/

<小时/>

为了纠正误解,这里有一个新版本,它完全可以满足您的需求:

jQuery(function() {

var myregex = /[﴾\u0660-\u0669﴿]+/;

$('span').each(function(index) {

var text = $(this).text();

$(this).text('');

var matches = text.match(myregex);

var content = text.replace(myregex, '');

//alert(content);
//alert(index + ': ' + $(this).text());
//alert(matches);

var newSpan = document.createElement('span');

$(newSpan).text(matches);

$(this).text(content);

$(newSpan).prependTo($(this));
});
});

对应的jsFiddle在这里:http://jsfiddle.net/leniel/YyAXP/2/

<小时/>

我希望这是您最后一次要求更改! :-)

var myregex = /([\u0660-\u0669]+)/g;

var parenthesis = /[﴾﴿]+/g;

var text = $("#sentences");

//alert(content);
//var matches = $(text).text().match(myregex);
//alert(text.html());

text.html(function(i, oldHTML) {

return oldHTML.replace(parenthesis, '').replace(myregex, '<span>$1</span>');
});

这是 jsFiddle:http://jsfiddle.net/leniel/G4SkV/4/

关于jquery - 使用正则表达式和 jQuery 选择阿拉伯文本中的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12937818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com