我希望将简单文本更改为包含重音字符的链接,而不替换实际字符。
例如,假设我有一些来自 Twitter 的数据,例如:
x #LoMásLeído #Cristiano costó más que Bale'
现在我正在使用代码:
var text = "#LoMásLeído #Cristiano costó más que Bale'";
text.replace(/\#(\w+)/g, '<a href="http://twitter.com/hashtag/$1">#$1</a>');
现在这会将 #Cristiano 更改为 #Cristiano 。
尽管在同样的情况下,它会将 #LoMásLeído 中断为 #LoM ásLeído,因为它会在 á 上破裂。
Note: I know that I can replace all accented characters with plain characters (like á with a) first and then can replace this hashtags with links, but the final result will then contain no original accented characters and will look like LoMasLeido. I want the final result as original text like: LoMásLeído
好的,我已经完成了它的实现
Its implementation in beautiful modules of Joomla by Webkul, looks great, have a look here
最佳答案
我认为这符合你的要求:
var text = "#LoMásLeído #Cristiano costó más que Bale'";
text.replace(/\#(\S+)/g, '<a href="http://twitter.com/hashtag/$1">#$1</a>');
它会查找 #
字符后除空格 (\S
) 之外的所有内容,而不仅仅是数字、下划线和字母 a-z (\w
)。问题可能是它还包含点、逗号或大括号等字符。如果您确实需要完整的 UTF-8 版本 \w
您可能需要像 XRegExp 这样的库。另请参阅http://xregexp.com/plugins/
关于jquery - 将文本替换为包含重音字符的链接,而不替换 jquery 中的实际字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35061879/