Javascript Regular Expressions match Chinese characters and special characters (the method for matching keywords in the Project)

Source: Internet
Author: User

In the project, we need to create a checkbox keyword to prevent repeated features. The idea of using JS is as follows:

1. Get the text of all checkpoints and put them in the array.

2. Input a keyword loop array and use indexof to judge

Note: special characters may appear in the text of the checkbox, so special characters should be removed.

 
VaRStr= "Keywrod ";
VaRCh =/[\ u4e00-\ u9fa5 \ uf900-\ ufa2d]/;VaRSymbol =/([. * +? ^ $ {} () | [\] \/\])/;For(VaRI = 0; I <Str. length; I ++){VaRC =Str. charat (I );If(Ch. Test (c )){
Alert (C );}
If (symbol. Test (c )){
Alert (c)
}}

------------------------------- Split line -----------------------

CompleteCodeAs shown in the following figure (loop judgment in the Code is a bit more, and my Js capabilities are limited. I hope someone can provide other solutions or methods ):

 
FunctionCheckkeywords (){VaRKeywordsarray =[];//Checkboxlist nameVaRObjarray = ['industrialcatalogue ', 'businesscatalogue', 'industrialcatalogue']; Keywordsarray=Getallkeywords (objarray, keywordsarray );VaRKeyword;

VaRCompanycataloguedesc = $ ("# companycataloguedesc"). Val (). tolowercase (); // get the input content

VaR Ch =/[\ u4e00-\ u9fa5 \ uf900-\ ufa2d]/;
VaR Symbol =/([. * +? ^ $ {} () | [\] \/\] [0-9])/;
VaR En =/[A-Z]/;
VaR Descstr = "";
For ( VaR I = 0; I <companycataloguedesc. length; I ++ ){
C = Companycataloguedesc. charat (I );
If ((! Symbol. Test (c) & Ch. Test (c) | En. Test (c )){
Descstr = descstr + C;
}
}
Descstr = descstr. touppercase ();  
For ( VaR I = 0; I <keywordsarray. length; I ++ ){
If (Keywordsarray [I]! = Undefined ){
Keyword = Keywordsarray [I];
If (Descstr. indexof (keyword)>-1 ){
Alert ( "The category is repeated. Please select from the above" );
$ ( "# Keytext" ). Focus ();
Return ;
}
}
}
}

 //  Obtain all Keywords of a page Element  Function  Getallkeywords (objarray, keywordsarray ){  VaR  Keyword;  For ( VaR I = 0; I <objarray. length; I ++ ) {$ ( "Input [name = '" + objarray [I] + "']"). Each (Function  () {Keyword = $ ("Label [for = '" + $ ( This ). ATTR ('id') + "']" ). Text (); keywordsarray. Push (getchildkeyword (keyword, keywordsarray ));})}  Return  Keywordsarray ;}  //  Obtains the keywords separated by special characters in an element.  Function  Getchildkeyword (keyword, keywordsarray ){  VaR S2 = "" ;  VaR Ch =/[\ u4e00-\ u9fa5 \ uf900-\ ufa2d]/ ;  VaR Symbol =/([. * +? ^ $ {} () | [\] \/\])/ ;  For ( VaR I = 0; I <keyword. length; I ++ ){  VaR C = Keyword. charat (I );  If (Symbol. Test (c )&&! Ch. Test (c) {S2 = Keyword. substr (0 , I); keyword = Keyword. substr (I + 1, Keyword. Length );  If (S2! = "" ) {Keywordsarray. Push (getchildkeyword (keyword, keywordsarray ));}}  Else  {S2 = S2 + C ;}}  If (S2! = "" ){  Return  S2 ;}} 

------------------------------- Split line -----------------------

Originally, we wanted to use Lucene in the background to use Word Segmentation for judgment. However, Lucene was unfamiliar with it and the customer urged it. Therefore, we used Js. Try Lucene if you have time.

 

Modification description:

filter out the characters and numbers in the retrieved input content companycataloguedesc, and then match the keywords. Code comparison, which can be reconstructed. Do not copy + C/V directly, and apply it after reconstruction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.