ch1*_*1p_ 4 javascript reactjs react-jsx
替换字符串中的url并将它们作为React链接呈现的正确方法是什么?
说我有一个字符串:'hello http://google.com world',我希望它呈现如下:hello <a href="http://google.com">http://google.com</a> world
Joe*_*ddy 13
我在这里的每个答案都遇到了问题,所以我不得不写自己的:
// use whatever you want here
const URL_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
const renderText = txt =>
txt
.split(" ")
.map(part =>
URL_REGEX.test(part) ? <a href={part}>{part} </a> : part + " "
);
Run Code Online (Sandbox Code Playgroud)
好的,这就是我做的方式.
class A extends React.Component {
renderText() {
let parts = this.props.text.split(re) // re is a matching regular expression
for (let i = 1; i < parts.length; i += 2) {
parts[i] = <a key={'link' + i} href={parts[i]}>{parts[i]}</a>
}
return parts
}
render() {
let text = this.renderText()
return (
<div className="some_text_class">{text}</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
有 NPM 模块可以处理这个问题。这两者都依赖于linkify-it ( repo )
<Linkify>
<div>react-linkify <span>(tasti.github.io/react-linkify/)</span></div>
<div>React component to parse links (urls, emails, etc.) in text into clickable links</div>
See examples at tasti.github.io/react-linkify/.
<footer>Contact: tasti@zakarie.com</footer>
</Linkify>
Run Code Online (Sandbox Code Playgroud)
在撰写本文时,当前版本是 1.0.0-alpha。它需要 React 16。该存储库有 14 个开放票证和 17 个开放 PR。所以这并不好。
版本 0.2.2 允许更早的版本,但没有链接文本装饰等。
如果您使用本机(即手机应用程序),它看起来是两个选项中更好的一个。代码示例:
<Hyperlink linkDefault={ true }>
<Text style={ { fontSize: 15 } }>
This text will be parsed to check for clickable strings like https://github.com/obipawan/hyperlink and made clickable.
</Text>
</Hyperlink>
<Hyperlink onLongPress={ (url, text) => alert(url + ", " + text) }>
<Text style={ { fontSize: 15 } }>
This text will be parsed to check for clickable strings like https://github.com/obipawan/hyperlink and made clickable for long click.
</Text>
</Hyperlink>
<Hyperlink
linkDefault
injectViewProps={ url => ({
testID: url === 'http://link.com' ? 'id1' : 'id2' ,
style: url === 'https://link.com' ? { color: 'red' } : { color: 'blue' },
//any other props you wish to pass to the component
}) }
>
<Text>You can pass props to clickable components matched by url.
<Text>This url looks red https://link.com
</Text> and this url looks blue https://link2.com </Text>
</Hyperlink>
Run Code Online (Sandbox Code Playgroud)
试试这个库,它正是你需要的:https : //www.npmjs.com/package/react-process-string
那里的一个例子:
const processString = require('react-process-string');
let config = [{
regex: /(http|https):\/\/(\S+)\.([a-z]{2,}?)(.*?)( |\,|$|\.)/gim,
fn: (key, result) => <span key={key}>
<a target="_blank" href={`${result[1]}://${result[2]}.${result[3]}${result[4]}`}>{result[2]}.{result[3]}{result[4]}</a>{result[5]}
</span>
}, {
regex: /(\S+)\.([a-z]{2,}?)(.*?)( |\,|$|\.)/gim,
fn: (key, result) => <span key={key}>
<a target="_blank" href={`http://${result[1]}.${result[2]}${result[3]}`}>{result[1]}.{result[2]}{result[3]}</a>{result[4]}
</span>
}];
let stringWithLinks = "Watch this on youtube.com";
let processed = processString(config)(stringWithLinks);
return (
<div>Hello world! {processed}</div>
);
Run Code Online (Sandbox Code Playgroud)
这将替换所有带有或不带有“http://”协议的链接。如果您只想用协议替换链接,请从配置数组中删除第二个对象。
| 归档时间: |
|
| 查看次数: |
5151 次 |
| 最近记录: |