在经历this时,我遇到了以下代码:
// src/routes.js
import React from 'react';
import { Route, Router } from 'react-router-dom';
import App from './App';
import Home from './Home/Home';
import Callback from './Callback/Callback';
import Auth from './Auth/Auth';
import history from './history';
const auth = new Auth();
const handleAuthentication = (nextState, replace) => {
if (/access_token|id_token|error/.test(nextState.location.hash)) {
auth.handleAuthentication();
}
}
export const makeMainRoutes = () => {
return (
<Router history={history} component={App}>
<div>
<Route path="/" render={(props) => <App auth={auth} {...props} />} />
<Route path="/home" render={(props) => <Home auth={auth} {...props} />} />
<Route path="/callback" render={(props) => {
handleAuthentication(props);
return <Callback {...props} />
}}/>
</div>
</Router>
);
}
在handleAuthentication中,我不明白/access_token|id_token|error/这个符号是什么。请解释。代码的 GitHub 链接是 this
最佳答案
这是一个Regular Expression .
|
(或)表示它与 access_token
、id_token
或 error
匹配:
let re = /access_token|id_token|error/;
console.log(re.test("access_token")) // true
console.log(re.test("something")) // false
console.log(re.test("id_token")) // true
使用 .test
来测试传递给它的字符串并评估它是否适合表达式。
关于javascript -/x|y|z/的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52296789/