3

This is perhaps a tricky one. Using a regex, I want to select the Arabic numbers (can be 1, 2 or 3 Arabic digits) between parentheses as well as the parentheses themselves here: http://jsfiddle.net/QL4Kr/.

<span>
    كهيعص ﴿١﴾
</span>
<span>
    وَإِلَيْهِ تُرْ‌جَعُونَ ﴿٢٤٥﴾
</span>​
jQuery(function () {    
    var oldHtml = jQuery('span').html();
    var newHtml = oldHtml.replace("","");
    jQuery('span').html(newHtml); });​
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
drake035
  • 3,955
  • 41
  • 119
  • 229
  • What do you mean "select patterns"? What do you want to do with them? And how is this different from the question you just posted yesterday? http://stackoverflow.com/questions/12917472/selecting-parenthesis-in-arabic-text – NullUserException Oct 17 '12 at 15:44
  • The body of my question explains « patterns » in the title (individual, pairs or triplets of digits between parentheses), and my previous question was about selecting parentheses only. – drake035 Oct 17 '12 at 17:53

1 Answers1

3

Here's what I came up with:

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);

    });

});

Here's the jsFiddle to play with it: http://jsfiddle.net/leniel/YyAXP/


Here's the modified version that keeps only the numbers inside each <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); 

    });

});

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


To correct the misunderstanding, here's a new version that does exactly what you want:

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));
    });
});

The corresponding jsFiddle is here: http://jsfiddle.net/leniel/YyAXP/2/


I hope this is your last requirement change! :-)

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>');
});

Here's the jsFiddle: http://jsfiddle.net/leniel/G4SkV/4/

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • Thank you Leniel, that looks good. Any way you can modify it so that the parentheses get replaced by span tags ? For this is my ultimate goal: putting those numbers on top of an image (a circle basically) to adorn them. – drake035 Oct 17 '12 at 17:55
  • So you want to remove the parentheses only but keep the number between the span tag? – Leniel Maccaferri Oct 17 '12 at 19:18
  • Yep, that's exactly what I want. – drake035 Oct 17 '12 at 20:31
  • thanks pal. I think there was a misunderstanding regarding span tags though: in my markup on http://jsfiddle.net/leniel/YyAXP/1/, my two sentences are between span tags, but I want the parentheses within theses sentences to be replaced by new span tags, like so: numberword word word word. So I wasn't refering to the span tags surrounding the sentences, but rather to new span tags taking place of parentheses. I apologize for the confusion! – drake035 Oct 18 '12 at 06:30
  • Leniel, you'll probably ignore me and I may even get sanctioned or something, but I made one more mistake in my markup here compared to how my web project is: all sentences are actually in a single element, not each one in a separate element. It's like so: http://jsfiddle.net/G4SkV/ (I used div elements to avoid confusion with the span elements I need around numbers). If you can adapt the code once more it would be super cool but I really won't blame you if you choose not to. – drake035 Oct 18 '12 at 19:38
  • Quite helpful. Thanks. I am trying to select all arabic letters. – Ibn Saeed Aug 05 '14 at 13:50