Lucy
Lucy

Reputation: 491

javaScript text validation by regex

I need to check if a string starts with http:// in java script

What I have is

if(!txt.match(/^http:/)) {}  // this only works for http:

I need to make it work for http://

can any one suggest a solution please?

Upvotes: 0

Views: 196

Answers (2)

jensgram
jensgram

Reputation: 31508

Alternatively:

txt.substr(0, 7) === "http://"

Upvotes: 2

antyrat
antyrat

Reputation: 27765

You need to escape \ chars, also for checking is better to use test method.

if(!(/^http:\/\//.test(txt))) {}

Upvotes: 1

Related Questions