html search and replace on server side

為{幸葍}努か 提交于 2019-12-11 09:40:03

问题


I like to search something like stack <"sometag"> overflow</"sometag"> and replace with stack <"sometag">underflow</"sometag">

It is part of a large html text and I like to do it in Java (there is some limitation on the server side technologies that I can use). I searched through and found this post: How to find/replace text in html while preserving html tags/structure

One of the answers there suggests marking with special markers, producing plain text and then using regex. Finally unmarking and getting back the string to html. But it assumes that the string comes at a given position in the text. I have no way of knowing where the strings will be and how many times it may repeat.

Of course direct usage of regular expression search and replace is not appropriate here since I need to preserve the html tags.

Thanks in advance!


回答1:


I would be somewhat wary of doing regexps to change an HTML file. Too many things would go wrong.

Are your HTML files XML compliant? (e.g., XHTML?). In that case, you might be better off doing XML level transformations, with either XSL or a query based parser.




回答2:


Take look on http://jsoup.org/ It does all kinds of stuff with tags.



来源:https://stackoverflow.com/questions/4696600/html-search-and-replace-on-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!