0

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

4
  • /Abcd \d\d:\d\d:\d\d/ (this requires it to be Abcd DIGITDIGIT:DIGITDIGIT:DIGITDIGIT). Commented Apr 25, 2014 at 16:06
  • Yes, why don't just include Abcd in the regex? Commented Apr 25, 2014 at 16:08
  • Thanks for you response. But how can i use it to search in the input string.The string Abcd keeps changing ,hence cannot really add it in regex Commented Apr 25, 2014 at 16:09
  • Currently i am checking the input string in two parts. var check=aqString.Find(Inputstring,"Abcd",0) var matches=regex.test(Inputstring) if(check !=1){ if(matches) //do something } Commented Apr 25, 2014 at 16:15

2 Answers 2

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.