简体   繁体   English

reactcomponent中的regex标签按名称在两个可能的标签中

[英]regex tag in reactcomponent by name in two possibles tag

I need an regex to find <Field ...name="document"> or <FieldArray ...name="document"> to replace with an empty string. 我需要一个正则表达式来找到<Field ...name="document"><FieldArray ...name="document">以替换为空字符串。 They can be defined across multiple lines. 可以跨多行定义它们。

This is not html or xhtml, it's just a text string containing <Field> and <FieldArray> 这不是html或xhtml,它只是包含<Field><FieldArray>的文本字符串

Example with Field: 字段示例:

      <Field
        component={FormField}
        name="document"
        typeInput="selectAutocomplete"
      />

Example with FieldArray: FieldArray示例:

      <FieldArray
        component={FormField}
        typeInput="selectAutocomplete"
        name="document"
      />

the are inside a list of components. 在组件列表中。 Example: 例:

      <Field
        name="amount"
        component={FormField}
        label={t('form.amount')}
      />
      <Field
        name="datereception"
        component={FormField}
        label={t('form.datereception')}
      />
      <Field
        component={FormField}
        name="document"
        typeInput="selectAutocomplete"
      />
      <Field
        name="datedeferred"
        component={FormField}
        label={t('form.datedeferred')}
      />

I've have read some solutions like to find src in Extract image src from a string but his structure is different a what i'm looing for. 我已经阅读了一些解决方案,例如在从字符串提取图像src中找到src,但是他的结构与我想要的不同。

It is not advisable to parse [X]HTML with regex . 不建议使用regex解析[X] HTML If you have a possibility to use a domparser, I would advise using that instead of regex. 如果您有可能使用domparser,我建议您使用它而不是regex。

If there is no other way, you could this approach to find and replace your data: 如果没有其他方法,则可以使用这种方法来查找和替换数据:

<Field(?:Array)?\\b(?=[^\\/>]+name="document")[^>]+\\/>

Explanation 说明

  • Match <Field with optional "Array" and end with a word boundary <Field(?:Array)?\\b <Field与可选的“ Array”匹配,并以单词边界<Field(?:Array)?\\b结尾
  • A positive lookahead (?= 正向前看(?=
  • Which asserts that following is not /> and encounters name="document" [^\\/>]+name="document" 它断言跟随不是/>并且遇到name =“ document” [^\\/>]+name="document"
  • Match not a > one or more times [^>]+ 不匹配>一次或多次[^>]+
  • Match \\/> 匹配\\/>

 var str = `<Field name="amount" component={FormField} label={t('form.amount')} /> <Field name="datereception" component={FormField} label={t('form.datereception')} /> <Field component={FormField} name="document" typeInput="selectAutocomplete" /> <Field name="datedeferred" component={FormField} label={t('form.datedeferred')} /> <FieldArray component={FormField} typeInput="selectAutocomplete" name="document" /><FieldArray component={FormField} typeInput="selectAutocomplete" name="document" />` ; str = str.replace(/<Field(?:Array)?\\b(?=[^\\/>]+name="document")[^>]+\\/>/g, ""); console.log(str); 

Here's an answer with actual XML parsing and no regular expressions: 这是使用实际XML解析并且不使用正则表达式的答案:

 var xml = document.createElement("xml"); xml.innerHTML = ` <Field name="amount" component={FormField} label={t('form.amount')} /> <FieldDistractor component={FormField} name="document" typeInput="selectAutocomplete" /> <Field name="datereception" component={FormField} label={t('form.datereception')} /> <Field component={FormField} name="document" typeInput="selectAutocomplete" /> <Field name="datedeferred" component={FormField} label={t('form.datedeferred')} /> <FieldArray component={FormField} typeInput="selectAutocomplete" name="document" /><FieldArray component={FormField} typeInput="selectAutocomplete" name="document" /> `; var match = xml.querySelectorAll( `field:not([name="document"]), fieldarray:not([name="document"]), :not(field):not(fieldarray)` ); var answer = ""; for (var m=0, ml=match.length; m<ml; m++) { // cloning the node removes children, working around the DOM bug answer += match[m].cloneNode().outerHTML + "\\n"; } console.log(answer); 

In writing this answer, I found a bug in the DOM parser for both Firefox ( Mozilla Core bug 1426224 ) and Chrome ( Chromium bug 796305 ) that didn't allow creating empty elements via innerHTML. 在编写此答案时,我在DOM分析器中发现了Firefox( Mozilla Core错误1426224 )和Chrome( Chromium错误796305 )的错误 ,该错误不允许通过innerHTML创建空元素。 My original answer used regular expressions to pre- and post-process the code to make it work, but using regexes on XML is so unsavory that I later changed it to merely strip off children by using cloneNode() (with its implicit deep=false ). 最初的答案是使用正则表达式对代码进行预处理和后处理以使其正常工作,但是在XML上使用正则表达式太麻烦了,以至于我后来将其更改为仅使用cloneNode()剥离子cloneNode()其隐式deep=false )。

So we dump the XML into a dummy DOM element (which we don't need to place anywhere), then we run querySelectorAll() to match some CSS that specifies your requirements: 因此,我们将XML转储到虚拟DOM元素中(不需要将其放置在任何地方),然后运行querySelectorAll()来匹配一些指定您要求的CSS:

  • field:not([name="document"]) "Field" elements lacking name="document" attributes, or field:not([name="document"]) “字段”元素缺少name="document"属性,或者
  • fieldarray:not([name="document"]) "FieldArray" elements lacking that attribute, or fieldarray:not([name="document"])缺少该属性的“ FieldArray”元素,或者
  • :not(field):not(fieldarray) Any other element :not(field):not(fieldarray)任何其他元素

You can parse HTML tags with regex because parsing the tags themselves are nothing special and are the first thing parsed as an atomic operation. 您可以使用regex解析HTML标记,因为解析标记本身并不特殊,并且首先将其解析为原子操作。

But, you can't use regex to go beyond the atomic tag. 但是,您不能使用正则表达式来超越原子标记。
For example, you can't find the balanced tag closing to match the open as 例如,您找不到平衡标签关闭以匹配打开为
this would put a tremendous strain on regex capability. 这将对正则表达式功能造成巨大压力。

What a Dom parser does is use regex to parse the tags, then uses internal Dom解析器的作用是使用正则表达式解析标签,然后使用内部
algorithms to create a tree and carry out processing instructions to interpret 创建树并执行处理指令以进行解释的算法
and recreate an image. 并重新创建图像。
And of course regex doesn't do that. 当然,正则表达式不会这样做。

Sticking to strictly parsing tags, including invisible content (like script), 坚持严格解析标签,包括不可见内容(如脚本),
is not that easy as well. 也不是那么容易。
Content can hide or embed tags that, when you look for them, you shouldn't 内容可以隐藏或嵌入标签,当您寻找它们时,您不应
find them. 找到他们。

So, in essence, you have to parse the entire html file to find the real 因此,从本质上讲,您必须分析整个 html文件才能找到真正的
tag your looking for. 标记您的寻找。
There is a general regex that can do this that I will not include here. 有一个通用的正则表达式可以做到这一点,我将不在此介绍。
But if you need it let me know. 但是,如果您需要它,请告诉我。

So, if you want to jump straight into the fire without parsing all the 因此,如果您想直接跳入火中而无需解析所有
tags of the entire file, this is the regex to use. 整个文件的标签,这是要使用的正则表达式。

It is essentially a cut up version of the one that parses all tags. 从本质上讲,它是解析所有标签的版本的简化版本。
This flavor finds the tag and any attribute=value that you need, 这种味道可以找到标记和所需的任何attribute = value
and also finds them out-of-order . 并且发现它们乱序
It can also be used to find out-of-order, multiple attr/val's within the same tag. 它还可以用于在同一标签中查找乱序的多个attr / val。

This is for your usage: 这是供您使用的:

/<Field(?:Array)?(?=(?:[^>"']|"[^"]*"|'[^']*')*?\sname\s*=\s*(?:(['"])\s*document\s*\1))\s+(?:"[\S\s]*?"|'[\S\s]*?'|[^>]*?)+\/>/

Explained/Formatted 解释/格式化

 < Field                # Field or  FieldArray  tag
 (?: Array )?

 (?=                    # Asserttion (a pseudo atomic group)
      (?: [^>"'] | " [^"]* " | ' [^']* ' )*?
      \s name \s* = \s* 
      (?:
           ( ['"] )               # (1), Quote
           \s* document \s*       # With name = "document"
           \1 
      )
 )
 \s+ 
 (?: " [\S\s]*? " | ' [\S\s]*? ' | [^>]*? )+
 />

Running demo: https://regex101.com/r/ieEBj8/1 运行演示: https : //regex101.com/r/ieEBj8/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM