Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How regular expressions deal with picture addresses and img tags

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "regular expression how to deal with picture address, img tag", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "regular expression how to deal with picture address, img tag" this article.

When uploading facial expressions or image addresses, most of the time, you can't upload them directly.

So you should process the img tag before uploading comments or pictures.

/ / the first step is to get the string var str = 'Picture 1 with the img tag

Picture 2

Picture 3

'

Process the obtained string

/ / replace the img tag with the expression var reg = /] * src [=\ "\'\ s] + [^\.] * / ([^\.])\. [^\"\'] + [\ "\']? [^ >] * > / gi; str = str.replace (reg," [face:$1.gif] "); console.log (str)

The result of the operation is:

Picture 1 [face:sanai.gif] Picture 2 [face:em_23.gif] Picture 3 [face:451.gif]

In this way, the code uploaded to the server will not have a html tag, which is a good way to deal with image comments and upload information to prohibit html tags.

Next:

The result of the run in the previous step returned from the server is parsed in reverse as follows:

/ / replace the specific encoding with the expression var regg = / face: ([\ w] +) .gif / gi; str = str.replace (regg, "

); console.log (str)

The running result is:

Picture 1

Picture 2

Picture 3

The html page of the code is attached below, which can be copied and pasted into the html file to run directly.

Var str = 'Picture 1

Picture 2

Picture 3

'; / / replace the img tag with the expression var reg = /] * src [=\ "\'\ s] + [^\.] * / ([^\.])\. [^\"\'] + [\ "\']? [^ >] * > / gi; str = str.replace (reg," [face:$1.gif] "); console.log (str) / / replace the specific encoding with the expression var regg = / face: ([\ w] +) .gif / gi; str = str.replace (regg, "

"); console.log (str); above is all the content of the article" how regular expressions deal with picture addresses and img tags ". Thank you for reading! I believe you all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report