有波斯字符时,如何更改textarea的方向? [英] How can I change the direction of textarea when there is Persian character?

查看:80
本文介绍了有波斯字符时,如何更改textarea的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:



$(body)。data-babel =false>

。 on('input','textarea',function(){var el = $(this); var len = el.val()。length; if(len <= 1){var x = new RegExp(如果(isAscii){el.css(direction,ltr); // is ascii var isAscii = x.test(el.val()。substring(0,1)); );} else {el.css(direction,rtl);}}});  

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script> < textarea>动态方向< / textarea>  

改变了这样一个textarea的方向。它基于第一个字符


  • 如果第一个字符是波斯字符或符号,它将 rlt 指向该textarea。

  • 如果第一个字符是英文字符,它会设置 lrt 指向该textarea。



那不是我想要的。 我想要这个:




  • 如果第一个字母 是英文字母,然后设置textarea ltr 方向。否则请设置 rtl






<以下是一些例子:

  var test =..!; // rtl 
var test =te; // ltr
var test =!te; // ltr
var test =..ق; // rtl
var test =مب; // rtl
var test =eس; // ltr
var test =سe; // rtl
var test =^Ëe; // rtl
var test =.32eس; // ltr

我该怎么做?

解决方案

如果我正确地阅读了这个问题,目标是如果第一个非符号/符号/标点符号是ASCII字符,则将文本从左到右读取,否则



我认为您所需要做的就是将您的正则表达式更改为首先匹配0个或更多符号/符号/标点符号,然后来测试下一个字符是否是ASCII字符。



正则表达式 [ - !$%^& *( )_ + |〜=`{} \ [\]:;'<>?,。\ /] 是符号/符号/标点符号的一个相当完整的正则表达式,在这里找到: https://stackoverflow.com/a/8359631/4132627 。您可能需要添加它为



把它们放在一起,我们会得到类似于 [ - !$%^& *()_ + |〜= `{} \ [\]:;'<>,\ /] * [A-ZA-Z] 。两个字符组之间的 * 表示匹配前一组的0个或更多。



我已经用那个正则表达式更新了你的代码片段,它看起来像预期的那样工作。也删除了长度检查,因为无论有多少字符都需要运行。



这可能并不完美 - 有很多情况可能会被忽略。你可能需要玩一下。例如,如果第二个字符组还包含数字( [A-Za-z0-9] )?




$ b

$(body ).on('input','textarea',function(){var el = $(this); var len = el.val()。length; // if(len <= 1){var x = / ^ [! - ?$%^&安培; *()_ + |〜='{} \ [\]:\;'<>,\ /] * [A-ZA-Z ] /; // is ascii var isAscii = x.test(el.val()); if(isAscii){el.css(direction,ltr);} else {el.css(direction, rtl);}}};

< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script>< textarea>动态方向< / textarea>


Here is my code:

$("body").on('input', 'textarea', function() {
  
      var el  = $(this);
      var len = el.val().length;
  
      if (len <= 1){
	    var x = new RegExp("[A-Za-z]"); // is ascii
	    var isAscii = x.test(el.val().substring(0, 1));
	    if(isAscii){
	    	el.css("direction", "ltr");
	    } else {
	    	el.css("direction", "rtl");
	    }
    }
  
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>dynamic direction</textarea>

My current code changes the direction of such a textarea. It is based on the first character:

  • If the first character is either a Persian character or a sign, it sets rlt direction to that textarea.
  • If the first character is a English character, it sets lrt direction to that textarea.

Well that's not what I want. I want this:

  • If the first letter (not signs) is a English letter, then set the textarea ltr direction. Otherwise set it rtl.

Here is some examples:

var test = "..!";    // rtl
var test = "te";     // ltr
var test = "!te";    // ltr
var test = "..ق";    // rtl
var test = "مب";     // rtl
var test = "eس";     // ltr
var test = "سe";     // rtl
var test = "^سe";    // rtl
var test = ".32eس";  // ltr

How can I do that?

解决方案

If I read the question correctly, the goal is to have the text read left-to-right if the first non-symbol/sign/punctuation character is an ASCII character, otherwise read right-to-left.

I think all you need to do is change your regex to first match 0 or more symbols/signs/punctuation-marks, and then to test if the next character is an ASCII character.

The regex [-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/] is a fairly complete regex for symbols/signs/punctuation-marks, found here: https://stackoverflow.com/a/8359631/4132627. You may need to add to it as you see fit.

Putting that together we'd get something like [-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]*[A-Za-z]. The * between the two character groups means "match 0 or more of the previous group".

I've updated your snippet with that regex and it appears to work as expected. Also removed the length check as this needs to run no matter how many characters there are.

This probably isn't perfect - there are many cases probably being left out. You may need to play with it a bit. For example, should that second character group also include numbers ([A-Za-z0-9])?

In any case, I hope this helps!

$("body").on('input', 'textarea', function() {
  
      var el  = $(this);
      var len = el.val().length;
  
      //if (len <= 1){
    	    var x = /^[-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]*[A-Za-z]/; // is ascii
  	  	    var isAscii = x.test(el.val());

		    if(isAscii){
		    	el.css("direction", "ltr");
		    } else {
		    	el.css("direction", "rtl");
	  	  }
    	//}
  
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>dynamic direction</textarea>

这篇关于有波斯字符时,如何更改textarea的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆