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.
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 .
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>
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,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.