SoFunction
Updated on 2025-04-06

Regular expressions Match at least one non-whitespace character and not exceed the specified length

chinmo reverse thinking solution
Copy the codeThe code is as follows:

<script type="text/javascript">
/**
* Regular expression with at least one non-whitespace character and no more than 6 characters
*
* Author: chinmo
* tidy:
* Source: /u/20090207/18/#r_55136904
*/
var pattern = /^[\s]{0,}$|^[\w\s]{7,}$/g;
var str = "";
var str1 = " ";
var str2 = "a";
var str3 = "abcdefgabcdefgabcdefgabcdefgg";
var str4 = " a ";
(!(str))
(!(str1))
(!(str2))
(!(str3))
(!(str4))
</script>

Regular expression rule analysis:
^[\s]{0,}$: The entire string is empty or is a whitespace character
^[\w\s]{7,}$: The entire string length is greater than 6
The author adopts reverse thinking, and achieves the required effect by matching situations that do not meet the conditions, and then negating (note the exclamation mark in each).
JK_10000 Reverse Thinking Solution Simplified Version
Copy the codeThe code is as follows:

<script type="text/javascript">
/**
* Regular expression with at least one non-whitespace character and no more than 6 characters
*
* Author: JK_10000
* tidy:
* Source: /u/20090207/18/#rt_55145516
*/
var pattern = /.{7}|^\s*$/g;
var str = "";
var str1 = " ";
var str2 = "a";
var str3 = "";
var str4 = " a ";
(!(str))
(!(str1))
(!(str2))
(!(str3))
(!(str4))
</script>

Regular expression rule analysis:
.{7}: The entire string length is greater than 6. Note: Here . matches any character
^\s*$: The entire string is empty or is a whitespace character
JK_10000 Positive thinking solution
Copy the codeThe code is as follows:

<script type="text/javascript">
/**
* Regular expression with at least one non-whitespace character and no more than 6 characters
*
* Author: JK_10000
* tidy:
* Source: /jkisjk/blog/item/
* Source: /u/20090207/18/#rt_55145611
*/
var pattern = /^(?!.{7}|^\s*$)/g;
var str = "";
var str1 = " ";
var str2 = "a";
var str3 = "";
var str4 = " a ";
((str))
((str1))
((str2))
((str3))
((str4))
</script>

Regular expression rule analysis:
.{7}: The entire string length is greater than 6. Note: Here . matches any character
^\s*$: The entire string is empty or is a whitespace character
The author used the order of regular expressions to negate the surrounding view, indicating that there cannot be 7 or more characters after the beginning (^), or the entire string is empty (if \s does not, ^$ means the content is empty), or all are blank characters (\s*).
However, this regular expression can remove ^ in the circumferential condition, i.e. /^(?!.{7}|\s*$)/g , because the rule already has a ^ from the beginning.
Copy the codeThe code is as follows:

<script type="text/javascript">
/**
* Regular expression with at least one non-whitespace character and no more than 6 characters
*
* Author: JK_10000
* tidy:
* Source: /jkisjk/blog/item/
* Source: /u/20090207/18/#rt_55145611
*/
var pattern = /^(?!.{7}|\s*$)/g;
var str = "";
var str1 = " ";
var str2 = "a";
var str3 = "";
var str4 = " a ";
((str))
((str1))
((str2))
((str3))
((str4))
</script>

wc Solutions posted in JK Blog Comments
Copy the codeThe code is as follows:

<script type="text/javascript">
/**
* Regular expression with at least one non-whitespace character and no more than 6 characters
*
* Author: wc
* tidy:
* Source: /jkisjk/blog/item/
*/
var pattern = /^(?=.*?\S)[\s\S]{0,6}$/g;
var str = "";
var str1 = " ";
var str2 = "a";
var str3 = "";
var str4 = " a ";
((str))
((str1))
((str2))
((str3))
((str4))
</script>

Regular expression rule analysis:
(?=.*?\S): Look around in order, specifying that any multiple characters are followed by a non-whitespace character
[\s\S]{0,6}: less than 6 blank or non-whitespace characters
Pay special attention when using the global matching modifier g in Javascript, and refer to another article on this site:Things to note when using exec to global match regular expressions in Javascript