문제

I have to verify if my string contains a substring followed by a regex.

Lets say the string looks like: "Abcd 03:02:01"

How can i verify that the string contains the substring "Abcd" and the regex for "03:02:01". I am using jscript in TC9.

I tried using find for the Substring and checked regex using Test.This involves checking the input string twice in If loops. But is there any other way to do this?

Thanks

도움이 되었습니까?

해결책 2

You can use variables in a regex expression.

var str = "Abcd 03:02:01";
var beginning = "Abcd"; 
var pattern= RegExp(beginning + " \\d\\d:\\d\\d\\:\\d\\d"); 
var result = pattern .test(str); //bool return

다른 팁

If I understood well your question, you want to match the existence of Abcd 03:02:01in a string, based on that, you can use:

var s = "Abcd 03:02:01";
if (/^Abcd \d{2}:\d{2}:\d{2}$/.test(s)) {
    // Successful match
} else {
    // Match attempt failed
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top