1

I want to write a regex to match whether my url has gmt.php or not. For example:

If my url is http://example.com/gmt.php?a=1 it is true

If my url is http://example.com/ac.php then it is false

I tried:

/^([a-z0-9])$/.test('gmt.php');

but its not perfect. And yes I need only regex not substring match indexOf Thanks.

1
  • "but its not perfect" - or in any way related to what you're trying to do... May I suggest that you google up a regex tutorial, and you'll find this is very easy to implement. Commented Mar 11, 2016 at 6:36

3 Answers 3

1

Why not simply indexOf

url.indexOf( "gmt.php" ) != -1 //outputs true if it exists

For regex (not sure why you want regex for such simple thing ;))

/gmt\.php/.test('http://example.com/gmt.php?a=1 ');

or

/gmt.php/.test('http://example.com/gmt.php?a=1 ');//since . is . outside []

/^([a-z0-9])$/.test('gmt.php');

but its not perfect.

Because /^([a-z0-9])$/ will only match one alpha-numeric character.

Sign up to request clarification or add additional context in comments.

4 Comments

it is not regex, I need regex. :)
Thanks but I need it because some regex was already written for it and I have to use in that flow :)
@BhuvneshGupta did you tried the regex I have shared?
This is working fine but I need some more correction. It should match if there is anything before or after gmt.php e.g if it is abgmt.phpa then it should also return true.
0

Try this instead: /gmt.php/.test(url)

<html>
<head><title>foo</title>
<script>
function foo(url) {
  alert(/gmt.php/.test(url));
}
</script>
</head>
<body>
<form>
<input type="text" id="text" size="40"><input type="button" onclick="foo(document.getElementById('text').value)">
</form>
</body>

Comments

0

var reg = /^(?:https?:\/\/\w+\.\w+\/)?\w+\.\w+\?\w+\=\w+$/;
var url1 = 'http://example.com/gmt.php?a=1';
var url2 = 'http://example.com/ac.php';
var url3 = 'gmt.php?a=1';
var url4 = 'gmt.php'
    console.log(reg.test(url1));
    console.log(reg.test(url2));
    console.log(reg.test(url3));
    console.log(reg.test(url4));

if have other fail data,please @me,

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.