Regular Expression matching must contain at least one non-blank character and cannot exceed the specified length.

Source: Internet
Author: User

Chinmo reverse thinking Solution Copy codeThe Code is as follows: <script type = "text/javascript">
/**
* A Regular Expression with at least one non-white space character and no more than 6 Characters
*
* Author: chinmo
* Finishing: http://www.CodeBit.cn
* Source: http://topic.csdn.net/u/20090207/18/ffa003ed-ecd4-40e0-b81f-36aa1fe46d85.html#r_55136904
*/
Var pattern =/^ [\ s] {0, }$ | ^ [\ w \ s] {7,} $/g;
Var str = "";
Var str1 = "";
Var str2 = "";
Var str3 = "abcdefgabcdefgabcdefgabcdefgg ";
Var str4 = "";
Document. write (! Pattern. test (str ))
Document. write (! Pattern. test (str1 ))
Document. write (! Pattern. test (str2 ))
Document. write (! Pattern. test (str3 ))
Document. write (! Pattern. test (str4 ))
</Script>

Regular Expression Rule Analysis:
^ [\ S] {0, }$: the entire string is null or is a blank character.
^ [\ W \ s] {7, }$: the length of the entire string is greater than 6
The author adopts reverse thinking, matching situations that do not meet the conditions, and then performing inverse operations (note the exclamation point in each document. write) to achieve the expected results.
JK_10000 reverse thinking solution simplified versionCopy codeThe Code is as follows: <script type = "text/javascript">
/**
* A Regular Expression with at least one non-white space character and no more than 6 Characters
*
* Author: JK_10000
* Finishing: http://www.CodeBit.cn
* Source: http://topic.csdn.net/u/20090207/18/ffa003ed-ecd4-40e0-b81f-36aa1fe46d85.html#rt_55145516
*/
Var pattern =/. {7} | ^ \ s * $/g;
Var str = "";
Var str1 = "";
Var str2 = "";
Var str3 = "www.CodeBit.cn ";
Var str4 = "";
Document. write (! Pattern. test (str ))
Document. write (! Pattern. test (str1 ))
Document. write (! Pattern. test (str2 ))
Document. write (! Pattern. test (str3 ))
Document. write (! Pattern. test (str4 ))
</Script>

Regular Expression Rule Analysis:
. {7}: the length of the entire string is greater than 6. Note:. Matches any character here.
^ \ S * $: the entire string is null or empty
JK_10000 positive thinking SolutionCopy codeThe Code is as follows: <script type = "text/javascript">
/**
* A Regular Expression with at least one non-white space character and no more than 6 Characters
*
* Author: JK_10000
* Finishing: http://www.CodeBit.cn
* Source: http://hi.baidu.com/jkisjk/blog/item/b54a7a3d1c7ce3c09f3d629b.html
* Source: http://topic.csdn.net/u/20090207/18/ffa003ed-ecd4-40e0-b81f-36aa1fe46d85.html#rt_55145611
*/
Var pattern =/^ (?!. {7} | ^ \ s * $)/g;
Var str = "";
Var str1 = "";
Var str2 = "";
Var str3 = "www.CodeBit.cn ";
Var str4 = "";
Document. write (pattern. test (str ))
Document. write (pattern. test (str1 ))
Document. write (pattern. test (str2 ))
Document. write (pattern. test (str3 ))
Document. write (pattern. test (str4 ))
</Script>

Regular Expression Rule Analysis:
. {7}: the length of the entire string is greater than 6. Note:. Matches any character here.
^ \ S * $: the entire string is null or empty
The author uses a regular expression to sequentially deny the view, indicating that the START (^) cannot be followed by 7 or more characters, or that the entire string is null (\ s is not available, ^ $ indicates that the content is empty), or all are blank characters (\ s *).
However, this regular expression can remove the ^ In the view condition, that is,/^ (?!. {7} | \ s * $)/g, because the rule has a ^ at the beginning.Copy codeThe Code is as follows: <script type = "text/javascript">
/**
* A Regular Expression with at least one non-white space character and no more than 6 Characters
*
* Author: JK_10000
* Finishing: http://www.CodeBit.cn
* Source: http://hi.baidu.com/jkisjk/blog/item/b54a7a3d1c7ce3c09f3d629b.html
* Source: http://topic.csdn.net/u/20090207/18/ffa003ed-ecd4-40e0-b81f-36aa1fe46d85.html#rt_55145611
*/
Var pattern =/^ (?!. {7} | \ s * $)/g;
Var str = "";
Var str1 = "";
Var str2 = "";
Var str3 = "www.CodeBit.cn ";
Var str4 = "";
Document. write (pattern. test (str ))
Document. write (pattern. test (str1 ))
Document. write (pattern. test (str2 ))
Document. write (pattern. test (str3 ))
Document. write (pattern. test (str4 ))
</Script>

Solutions for publishing wc in JK blog commentsCopy codeThe Code is as follows: <script type = "text/javascript">
/**
* A Regular Expression with at least one non-white space character and no more than 6 Characters
*
* Author: wc
* Finishing: http://www.CodeBit.cn
* Source: http://hi.baidu.com/jkisjk/blog/item/b54a7a3d1c7ce3c09f3d629b.html
*/
Var pattern =/^ (? = .*? \ S) [\ s \ S] {0, 6} $/g;
Var str = "";
Var str1 = "";
Var str2 = "";
Var str3 = "www.CodeBit.cn ";
Var str4 = "";
Document. write (pattern. test (str ))
Document. write (pattern. test (str1 ))
Document. write (pattern. test (str2 ))
Document. write (pattern. test (str3 ))
Document. write (pattern. test (str4 ))
</Script>

Regular Expression Rule Analysis:
(? = .*? \ S): It is a positive sequence of view. A non-blank character is followed by any number of characters.
[\ S \ S] {0, 6}: white space or non-white space characters within 6
Pay special attention when using the global match modifier g in Javascript. Refer to another article on this site: Considerations for using exec in Javascript for global matching of Regular Expressions

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.