2012-09-04 78 views
0

我不擅长表达式 我想匹配字符串下面的字符串。php正则表达式/ preg_match初学者

http://www.site.com/ * .js 

preg_match('(http://www\.site\.com/).*(\.js)',$html,$match); 

我知道这段代码是不正确的。 *表示任何扩展名为.js的文件。 任何人都可以引导我的表达。 对不起,如果有任何重复。

+0

的preg_match( “/ www.site.com + JS /”,$ HTML ,$匹配); –

回答

1

你必须使用分隔符,如在模式 '#', '@' 或 '/':

$url = 'http://www.site.com/javascript/test.js'; 

$preg_match = preg_match('#(http://www\.site\.com/)(.*)(\.js)#', $url, $matches); 

if($preg_match === 1) 
{ 
    var_dump($matches); 
    // displays : 
    // array 
    // 0 => string 'http://www.site.com/javascript/test.js' (length=38) 
    // 1 => string 'http://www.site.com/' (length=20) 
    // 2 => string 'javascript/test' (length=15) 
    // 3 => string '.js' (length=3) 
} 
else 
{ 
    // doesn't match 
}