Surround Hebrew and English text in div

后端 未结 3 1114
慢半拍i
慢半拍i 2021-01-12 04:45

I am trying to add a span tag around Hebrew and English sentence in a paragraph. E.g. \"so היי all whats up אתכם?\" will become :

[span]so[/span][span]היי         


        
3条回答
  •  清酒与你
    2021-01-12 05:13

    I think the Regex you want is something like [^a-z^\u0591-\u05F4^\s]. I'm not entirely sure how you want to handle spaces.

    My solution

    Copy str to a new var res, replacing any characters that aren't A-Z / Hebrew.
    Loop over any english (a-z) characters in str and wrap them in a span, using res.replace.
    Do the same again for the Hebrew characters.

    It's not quite 100%, but seems to work well enough IMO.

    var str = 'so היי all whats up אתכם?';
    var finalStr = str.replace(/([^a-z^\u0591-\u05F4^\s])/gi, '');
    
    var rgx = /([a-z ]+)/gi;
    var mat = str.match(rgx);
    
    for(var i=0; i < mat.length; ++i){
        var match = mat[i];
        finalStr = finalStr.replace(match.trim(),''+match.trim()+'');
    }
    
    rgx = /([\u0591-\u05F4 ]+)/gi;
    var mat = str.match(rgx);
    
    for(var i=0; i < mat.length; ++i){
        var match = mat[i];
        finalStr = finalStr.replace(match.trim(),''+match.trim()+'');
    }
    
    document.getElementById('res').innerHTML = finalStr;
    

    http://jsfiddle.net/daveSalomon/0ns6nuxy/1/

提交回复
热议问题