0

I would add class when I have different language on paragraph or div like arabic language to make anther font and direction and I would make it automatic .

<div class="paragraph">
    <p>عربي لغة عربية تغير الاتجاه هنا</p>
</div>
<div class="paragraph">
    <p>example</p>
</div>

<script type="text/javascript">
    var regoo = "example|assignment";
    var re = new RegExp(regoo, 'ig');

    if($('div, p').text().match(re)) {
         $(this).addClass('gold');
    }
</script>
yceruto
  • 9,230
  • 5
  • 38
  • 65
Hefm
  • 61
  • 3
  • 13

2 Answers2

2

It could be done like this

function HasArabicCharacters(text){
    var arregex = /[\u0600-\u06FF]/;
 return arregex.test(text);
}

var textBlock = document.querySelector('#app p');
if(HasArabicCharacters(textBlock.textContent)){
 textBlock.classList.add('arabic');
}
.arabic{
 color: green;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Arabic test</title>
</head>
<body>
<div id="app">
 <p class="text">عربي لغة عربية تغير الاتجاه هنا</p>
</div>
</body>
</html>

HasArabicCharacters function is collected from here Regular Expression For Arabic Language

Community
  • 1
  • 1
azs06
  • 3,467
  • 2
  • 21
  • 26
  • but i had one problem it`s implement on one article , i wanna for all like live post . http://codepen.io/hesham-farag/pen/jyWgyp – Hefm Jan 12 '17 at 12:34
  • here is the pen updated http://codepen.io/azs06/pen/dNMqdM, you have to use `querySelectorAll()` to select all nodes, then check for arabic language test on all nodes. Hope this helps. – azs06 Jan 12 '17 at 15:41
0

Use square brackets in your regex, and supply all the characters of the arabic language and it will match any of the characters supplied in any order. It is the same as (a|b|c|d) but less code. If it doesnt work try prefixing your characters with a backslash, because they might be classed as special characters. Note, if you use backslash within quotes then you need to use two backslashes per character.

example:

var re = new RegExp('[\\ه\\ن\\ا]','gi')
Steve Tomlin
  • 3,391
  • 3
  • 31
  • 63