duannv2081 2015-02-03 04:53
浏览 22

使用正则表达式替换PHP的CSS ID或类

I have the following CSS code:

#np-toolbar-l a{ position: relative; padding: 0px 8px; line-height: 30px; display: inline-block; font-size: 12px; }
#np-toolbar-l a:hover{ background-color: #EFEFEF; opacity: 0.7; }
#np-toolbar-l a *:hover{ cursor: pointer; }


/** Icon */
#np-toolbar [class^="icon-"]{ font-size: 18px; vertical-align: middle; margin-right: 5px; }

I am trying to replace np-toolbar to another ID,

I just want to match np-toolbar not np-toolbar-l or something like this,

here's a tutoral about how to capture all the IDs from CSS:

#-?[_a-zA-Z]+[_a-zA-Z0-9-]*(?=[^}]*\{)

source: Regex to match ids in a css file?

I couldn't find out how do I search a "compelete ID" by regex,

here's my example: (It'll still count np-toolbar-l in)

/(?<=(#|\.))(np-toolbar)+(?=( |:||\[))/

EDIT: ummm, jsut added some symbols behind it,

it looks like the answer which I am looking for

/(?<=(#|\.))(np-toolbar)+(?=( |:|\[|\{|\.|,))/
  • 写回答

2条回答 默认 最新

  • douduan9129 2015-02-03 05:17
    关注

    If you're looking for a specific ID, you can greatly simplify your regex:

    /(#np-toolbar)(?=[{.,\s])/g
    

    That should find the ID wherever it appears in the CSS. It first matches the ID exactly, then only allows { (for beginning the style), , (for listing selectors), . (for appended classes), or space/tab/newline characters afterwards, preventing it from matching IDs like np-toolbar-1 etc.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?