doubaoxue5788 2017-04-22 22:08
浏览 126
已采纳

验证string是否包含正则表达式字符串

I have the following string below which was extracted by DOM Document:

$scripts = $doc->getElementsByTagName('script');
$script = $scripts->item($i); 
$string = $script->getAttribute('src');// Save the string 'jquery-3.2.0.min.js'

I have 3 ways to check if this string is a jquery:

jquery(?:\\-|\\.)([\\d.]*\\d)[^/]*\\.js\\;version:\\1
/([\\d.]+)/jquery(?:\\.min)?\\.js\\;version:\\1
jquery.*\\.js

In that case how can I validate the above string using one of these 3 commands?

  • 写回答

2条回答 默认 最新

  • duanlinghe8417 2017-04-22 22:13
    关注

    Use preg_match

    preg_match — Perform a regular expression match

    if (preg_match("/jquery.*\.js/",$string)) {
        echo "Match found";
    }
    

    If it finds a match it will print out Match found. (I just used one of your regex strings)

    I'm also assuming that $string is what you extracted, because it was hard to tell.

    Try it out

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制