[JavaScript] judge whether a string is a numeric value / judge whether it contains letters (with common regular expressions)

JavaScript usage example

If it is a value (number), return its value; if it is not a number, return the empty string ''.
It can avoid fetching null values from the database, which results in error reporting in the front-end display.

The autoConvertNum() function is defined as follows:

// If it is a value (number), return its value; if it is not a number, return the empty string ''
function autoConvertNum(numStr) {
	if (typeof (numStr) != 'string') {
		numStr = "" + numStr;
	}
	var e = numStr.match(/[a-z]/ig);  // Matching letters
	if (!!e) {//Containing letters
		return '';
	} else {
		return eval(numStr);
	}
}

Call method:

Attachment: common regular expressions:

1.A string of numbers, 26 letters, or underscores:
    ^[0-9a-zA-Z_]{1,}$
2.Nonnegative integer (positive integer + 0 ):
    ^/d+$
3. positive integer:
    ^[0-9]*[1-9][0-9]*$
4.Non positive integer (negative integer + 0): 
    ^((-/d+)|(0+))$
5. Negtive integer :
    ^-[0-9]*[1-9][0-9]*$
6.integer:    
    ^-?/d+$
7.Non negative floating point number (positive floating point number + 0):
    ^/d+(/./d+)?$
8.Positive floating point :
    ^(([0-9]+/.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*/.[0-9]+)|([0-9]*[1-9][0-9]*))$
9. Non positive floating point number (negative floating point number + 0):
    ^((-/d+(/./d+)?)|(0+(/.0+)?))$
10.Negative floating point number :
    ^(-(([0-9]+/.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*/.[0-9]+)|([0-9]*[1-9][0-9]*)))$
11. Floating point number :
    ^(-?/d+)(/./d+)?$
12.A string of 26 English letters :    
    ^[A-Za-z]+$
13. A string of 26 uppercase letters :
    ^[A-Z]+$
14.A string of 26 English letters in lowercase :
    ^[a-z]+$
15. A string of numbers and 26 English letters :
    ^[A-Za-z0-9]+$
16.A string of numbers, 26 letters, or underscores :    
    ^/w+$
17.email address :
    ^[/w-]+(/.[/w-]+)*@[/w-]+(/.[/w-]+)+$
18.url:    
    ^[a-zA-z]+://(/w+(-/w+)*)(/.(/w+(-/w+)*))*(/?/S*)?$
19. year-month-day:
    /^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/
20.month/day/year:
    /^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/
21.Emil:
    ^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$
22. Phone number:
    (d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?
23.IP address:
    ^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$
24. Regular expression matching Chinese characters:
    [/u4e00-/u9fa5]
25.Match double byte characters(Including Chinese characters): 
    [^/x00-/xff]
26. Regular expression matching empty rows:
    /n[/s| ]*/r
27.matching HTML Tagged regular expression:
    /<(.*)>.*<///1>|<(.*) //>/
28.Regular expressions that match leading and trailing spaces:
    (^/s*)|(/s*$)
29.matching Email Regular expression for address:
    /w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*
30. Match URL URL Regular expression for:
    ^[a-zA-z]+://(//w+(-//w+)*)(//.(//w+(-//w+)*))*(//?//S*)?$
31. Whether matching account number is legal(Letter start, allow 5-16 Byte, alphanumeric underline allowed): 
    ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
32. Match domestic phone number:
    (/d{3}-|/d{4}-)?(/d{8}|/d{7})?
33.Matching Tencent QQ Number:
    ^[1-9]*[1-9][0-9]*$
34. Only numbers can be entered:
    ^[0-9]*$
35.Input only n Digits:
    ^/d{n}$
36.You can only enter at least n Digits:
    ^/d{n,}$
37.Input only m~n Digits:
    ^/d{m,n}$
38.Only numbers beginning with zero and non-zero can be entered:
    ^(0|[1-9][0-9]*)$
39.Only positive real numbers with two decimal places can be entered:
    ^[0-9]+(.[0-9]{2})?$
40. Only 1 can be entered~3 Positive real number of decimal places:
    ^[0-9]+(.[0-9]{1,3})?$
41.Only non-zero positive integers can be entered:
    ^/+?[1-9][0-9]*$
42. Only non-zero negative integers can be entered:
    ^/-[1-9][0-9]*$
43.Only characters of length 3 can be entered:
    ^.{3}$
44. You can only enter a string of 26 English letters:
    ^[A-Za-z]+$
45.You can only enter a string of 26 uppercase letters:
    ^[A-Z]+$
46. You can only enter a string of 26 lowercase letters:
    ^[a-z]+$
47.Only strings consisting of numbers and 26 English letters can be entered:
    ^[A-Za-z0-9]+$
48. You can only enter a string consisting of numbers and 26 English letters or underscores:
    ^/w+$
49.Verify user password(The correct format is: start with a letter and the length is 5~17 Can only contain characters, numbers and underscores)
    ^[a-zA-Z]/w{5,17}$
50.Verify that there are ^%&',;=?$/"Equal characters:
    [^%&',;=?$/x22]+
51.Only Chinese characters can be entered:
    ^[/u4e00-/u9fa5]{0,}$
52,Only contain Chinese characters, numbers, letters and underscores, and cannot start or end with an underscore
    ^(?!_)(?!.*?_$)[a-zA-Z0-9_/u4e00-/u9fa5]+$
53,It only contains Chinese characters, numbers, letters and underscores, and there is no limit to the position of underscores
    ^[a-zA-Z0-9_/u4e00-/u9fa5]+$
54,2~4 Chinese character
    @"^[/u4E00-/u9FA5]{2,4}$
555 original articles published, 202 praised, 190000 visitors+
His message board follow

Keywords: Javascript Database Spring

Added by DapperDanMan on Tue, 21 Jan 2020 19:21:08 +0200