I want to edit a pattern that checks if text starts with number/s and ends with number/s or operator and return true or false based on test() method.
How can I do that with JavaScript ?`
var str= "123+125",str2 = "123",str3 = "123*";
let RegularExpression = /^\d*(\d*|(\+|\-|\*|\/){1,})$/; //this pattern variable what I want to edit because it doesn't do what I want.
let result1 = RegularExpression.test(str);
let result2 = RegularExpression.test(str);
let result2 = RegularExpression.test(str);
You could use repeat 1 or more times matching 1 or more digits, optionally followed by an operator.
^(?:\d+[+*/-]?)+
Explanation
^
Start of string (?:
Non capture group
\d+[+*/-]?
Match 1+ digits optionally followed by either +
*
/
-
)+
Close non capture group and repeat 1+ times let pattern = /^(?:\d+[+*/-]?)+$/m; [ "123+125", "", "123", "123*", "123+125-1" ].forEach(s => console.log(s + " --> " + pattern.test(s)));
Try this pattern /^(\d*|[+/*-]?){1,}$/
var str= "123+125",str2 = "123",str3 = "123*"; let RegularExpression = /^(\d*|[+/*-]?){1,}$/; //this pattern variable what I want to edit because it doesn't do what I want. let result1 = RegularExpression.test(str); let result2 = RegularExpression.test(str); let result3 = RegularExpression.test(str); console.log("res1:",result1,",res3:",result2,",res3:",result3)
Use
/^\d+([-+*\/]\d+)*[-+*\/]?$/
See proof
EXPLANATION:
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1 (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[-+*\/] any character of: '-', '+', '*', '\/'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
)* end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
--------------------------------------------------------------------------------
[-+*\/]? any character of: '-', '+', '*', '\/'
(optional (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
JavaScript:
var str= "123+125",str2 = "123",str3 = "123*"; let RegularExpression = /^\d+([-+*\/]\d+)*[-+*\/]?$/; console.log( RegularExpression.test(str) ); console.log( RegularExpression.test(str2) ); console.log( RegularExpression.test(str3) );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.