简体   繁体   English

正则表达式用字符串中的html标记替换字符

[英]regex replace characters with html tag in string

I'm getting with regex words I need to replace with input tag. 我遇到了需要用输入标签替换的正则表达式单词。 But when I'm adding html , it reads as plain string. 但是,当我添加html时,它读取为纯字符串。
Here is my string: "Hello, my name is /# Ann #/. I'm working for /# IStaff #/, could you please call me back". 这是我的字符串:“您好,我的名字是/#Ann#/。我正在为/#IStaff#/工作,请您给我回电话”。 Where i'm doing mistake? 我在哪里做错了?

editModalText() {
  const { modalMessage } = this.state
  let regex = /\/#(.*?)#\//g
  let replaced = modalMessage.replace(regex, '<input type="text">')
  return replaced
}

<div>{this.editModalText()}</div>

This is because it is a plain string. 这是因为它纯字符串。

You're going to have to change your approach entirely if you want to mix JSX syntax into your template. 如果要将JSX语法混合到模板中,则必须完全更改方法。

For example, build a tokenizer that would convert your string into an array like: 例如,构建一个令牌生成器 ,它将您的字符串转换为类似以下的数组:

[
    "Hello, my name is ",
    "/# Ann #/",
    ". I'm working for ",
    "/# IStaff #/",
    ", could you please call me back"
]

Then loop over it and push either the raw string or an appropriate JSX element into a new array, and finally return that. 然后循环遍历并将原始字符串或适当的JSX元素推入新数组,最后返回该数组。

You can match the middle part by using .*? 您可以使用.*?来匹配中间部分.*? .

 let modalMessage = "Hello, my name is /# Ann #/. I'm working for /# IStaff #/, could you please call me back"; let regex = /\\/#(.*?)#\\//g; let replaced = modalMessage.replace(regex, "<input />"); console.log(replaced); 

Also you need to escape your / in the regex with a backslash (\\). 另外,您还需要在正则表达式中使用反斜杠(\\)来转义/
A valid expression starts and ends with / : 有效表达式以/开头和结尾:

/<regex>/<flags>

So in your example: 因此,在您的示例中:

/          // <- Start of Regular expression
  \/       // <- Match /
  #        // <- Match #
    (.*?)  // <- Match the middle part (non-greedy)
  #        // <- Match #
  \/       // <- Match /
/g         // <- End regex and set "global" flag

Example with HTML: HTML示例:

 let modalMessage = "Hello, my name is /# Ann #/. I'm working for /# IStaff #/, could you please call me back"; let regex = /\\/#(.*?)#\\//g; let replaced = modalMessage.replace(regex, '<input type="text">'); document.write(replaced); 

You aren't catching the / around the placeholders (/ being a regex special character that needs to be escaped) 您不会在占位符周围引起误解(/是需要转义的正则表达式特殊字符)

using (.+) or (.*) will provide a greedy match. 使用(.+)(.*)将提供贪婪的匹配。 It means, it will match as much as possible, that's why you are matching too much. 这意味着,它将尽可能匹配,这就是为什么匹配太多的原因。 You want to use a lazy match (the opposite of greedy, that stops as soon as possible), (.+?) or (.*?) 您要使用惰性匹配(贪婪的反面,它会尽快停止), (.+?)(.*?)

You can use this one : 您可以使用以下一个:

 let modalMessage = "Hello, my name is /# Ann #/. I'm working for /# IStaff #/, could you please call me back" let regex = /\\/#(.*?)#\\//g let replaced = modalMessage.replace(regex, '<Input />') console.log(replaced) 

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

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