A regular expression matches at least one non-white-space character and does not exceed the specified length _ Regular expression

Source: Internet
Author: User
CHINMO Reverse Thinking Solution
Copy Code code as follows:

<script type= "Text/javascript" >
/**
* There is at least one non-white-space character and no more than 6-character regular expression
*
* 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 = "a";
var str3 = "Abcdefgabcdefgabcdefgabcdefgg";
var STR4 = "a";
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 empty or is a blank character
^[\w\s]{7,}$: The entire string length is greater than 6
The author uses the reverse thinking, by matching the conditions of the mismatch, and then through the reverse (note the exclamation point in each document.write) to achieve the desired effect.
jk_10000 Reverse Thinking Solution simplified version
Copy Code code as follows:

<script type= "Text/javascript" >
/**
* There is at least one non-white-space character and no more than 6-character regular expression
*
* 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 = "a";
var str3 = "www.CodeBit.cn";
var STR4 = "a";
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 entire string length is greater than 6, note: here. Match any character
^\s*$: The entire string is empty or is a blank character
jk_10000 Solutions for Positive Thinking
Copy Code code as follows:

<script type= "Text/javascript" >
/**
* There is at least one non-white-space character and no more than 6-character regular expression
*
* 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 = "a";
var str3 = "www.CodeBit.cn";
var STR4 = "a";
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 entire string length is greater than 6, note: here. Match any character
^\s*$: The entire string is empty or is a blank character
The author uses the sequential negation of the regular expression to look around, indicating that there can be no more than 7 characters after the start (^), or that the entire string is empty (the ^$ representation is empty if \s is not), or that all are whitespace characters (\s*).
However, the regular expression can be removed from the look in the condition, that is, the/^ (?!. {7}|\s*$)/g, because the rules were already in the beginning of a ^.
Copy Code code as follows:

<script type= "Text/javascript" >
/**
* There is at least one non-white-space character and no more than 6-character regular expression
*
* 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 = "a";
var str3 = "www.CodeBit.cn";
var STR4 = "a";
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>

WC published in the JK blog comments in the program
Copy Code code as follows:

<script type= "Text/javascript" >
/**
* There is at least one non-white-space character and no more than 6-character regular expression
*
* 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 = "a";
var str3 = "www.CodeBit.cn";
var STR4 = "a";
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): Sure order look around, specify any number of any characters followed by a non-white-space character
[\s\s] {0,6}: White or non-white space characters less than 6
When using the global matching modifier g in JavaScript, you should pay special attention to the other article in this site: Considerations for Regular expression global matching using exec in JavaScript

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.