如何在实时模板中使用regularExpression函数?

时间:2019-02-05 00:04:32

标签: reactjs webstorm live-templates

  1. 我试图使用正则表达式从剪贴板插入值。怎么做?
  2. 是否可以使用正则表达式删除具有属性的svg标签?

我试图写regularExpression(SVG, width="(\d+)px", $1)及其变体。

例如

我已经把它放在剪贴板中

    <svg width="32px" height="32px" viewBox="0 0 32 32" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <g transform="translate(-932.000000, -4274.000000)">
          <g transform="translate(0.000000, 4184.000000)">
            <g transform="translate(932.000000, 90.000000)">
              <rect fill="#3C5494" x="0" y="0" width="32" height="32" rx="16"></rect>
              <path d="M17.1973392,16.7117794 L19.5654279,16.7117794 L19.9199468,13.8590668 L17.1973392,13.8590668 L17.1973392,12.0421097 C17.1973392,11.2181543 17.4178625,10.6566668 18.5566829,10.6566668 L20,10.6559859 L20,8.11206559 C19.7488603,8.07744655 18.887255,8 17.8848071,8 C15.7919823,8 14.3592018,9.32549911 14.3592018,11.759728 L14.3592018,13.8590668 L12,13.8590668 L12,16.7117794 L14.3592018,16.7117794 L14.3592018,24 L17.1973392,24 L17.1973392,16.7117794 Z" fill="#FFFFFE"></path>
            </g>
          </g>
        </g>
      </g>
    </svg>

我使用此模板

/* eslint-disable max-len */
import React from 'react'
import Icon from './Icon'

const $NAME$ = ({ width, height }) => (
  <Icon
    width={width}
    height={height}
    viewBox='0 0 $width$ $height$'
  >
    $SVG$$END$
  </Icon>
)

export default $NAME$

image

我正在尝试从svg属性中插入$ width $和$ height $

除了v​​iewBox是'0 0 32 32'之外,我得到了'0 0 '

1 个答案:

答案 0 :(得分:1)

regularExpression(String, Pattern, Replacement)实时模板功能以以下方式运行:String中与Pattern匹配的所有匹配项均被第三个参数替换,并返回结果字符串(请参见函数实现)在https://github.com/JetBrains/intellij-community/blob/master/platform/lang-impl/src/com/intellij/codeInsight/template/macro/RegExMacro.java中)。因此,regularExpression(SVG, width="(\d+)px", $1)的结果是剪贴板内容由width="32px"替换为32