RNK
RNK

Reputation: 5792

Match starting characters of string

I have string like:

/transaction/tran_id=123

I want to match /transaction/ part of the string. if it's so then,

window.location.href = url;

else

alert("Not URL");

I tried with str.match("^string")

Upvotes: 0

Views: 275

Answers (2)

Will
Will

Reputation: 211

/^/\w/$/.test(str) tell can tell you if your string contain /anyword/, which will be more general. Hope it can help you.

Upvotes: 0

adeneo
adeneo

Reputation: 318162

if ( url.indexOf('/transaction/') === 0 )
    window.location.href = url;

You don't need a regex, just use indexOf, if the index is zero, the url starts with that string

Upvotes: 2

Related Questions