javascript正则找script标签, link标签里面的src或者 href属性
时间: 2023-08-31 18:26:00 浏览: 182
可以使用以下正则表达式来找到script标签和link标签中的src或href属性:
```javascript
/(?:<script|<link).*?(?:src|href)\s*=\s*['"]([^'"]*)['"]/gi
```
这个正则表达式会匹配以`<script`或`<link`开头的标签,然后找到紧接着的src或href属性,最后提取出属性值。使用`g`标志可以匹配所有符合条件的内容。
相关问题
用JavaScript写一个登录注册两个页面 要运用到正则表达式
好的,下面是一个简单的登录注册两个页面的实现,包括正则表达式的运用。
首先是HTML代码,包括两个页面的布局和表单:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login and Register</title>
</head>
<body>
<div id="login-page">
<h1>Login</h1>
<form>
<input type="text" placeholder="Username" id="login-username">
<input type="password" placeholder="Password" id="login-password">
<button type="submit" id="login-button">Login</button>
</form>
<p>Don't have an account? <a href="#" id="register-link">Register</a></p>
</div>
<div id="register-page" style="display:none;">
<h1>Register</h1>
<form>
<input type="text" placeholder="Username" id="register-username">
<input type="password" placeholder="Password" id="register-password">
<input type="password" placeholder="Confirm Password" id="register-confirm-password">
<button type="submit" id="register-button">Register</button>
</form>
<p>Already have an account? <a href="#" id="login-link">Login</a></p>
</div>
<script src="login-register.js"></script>
</body>
</html>
```
接下来是JavaScript代码,包括正则表达式的运用:
```javascript
// 匹配用户名和密码的正则表达式
const usernameRegex = /^[a-zA-Z0-9_-]{3,16}$/;
const passwordRegex = /^[a-zA-Z0-9_-]{6,18}$/;
// 登录页面DOM元素
const loginPage = document.getElementById('login-page');
const loginUsername = document.getElementById('login-username');
const loginPassword = document.getElementById('login-password');
const loginButton = document.getElementById('login-button');
const registerLink = document.getElementById('register-link');
// 注册页面DOM元素
const registerPage = document.getElementById('register-page');
const registerUsername = document.getElementById('register-username');
const registerPassword = document.getElementById('register-password');
const registerConfirmPassword = document.getElementById('register-confirm-password');
const registerButton = document.getElementById('register-button');
const loginLink = document.getElementById('login-link');
// 切换到登录页面
function showLoginPage() {
loginPage.style.display = 'block';
registerPage.style.display = 'none';
}
// 切换到注册页面
function showRegisterPage() {
loginPage.style.display = 'none';
registerPage.style.display = 'block';
}
// 验证登录表单
function validateLoginForm() {
const username = loginUsername.value.trim();
const password = loginPassword.value.trim();
if (!usernameRegex.test(username)) {
alert('Invalid username! Username should contain only letters, digits, underscores and hyphens, and should be 3-16 characters long.');
return false;
}
if (!passwordRegex.test(password)) {
alert('Invalid password! Password should contain only letters, digits, underscores and hyphens, and should be 6-18 characters long.');
return false;
}
return true;
}
// 验证注册表单
function validateRegisterForm() {
const username = registerUsername.value.trim();
const password = registerPassword.value.trim();
const confirmPassword = registerConfirmPassword.value.trim();
if (!usernameRegex.test(username)) {
alert('Invalid username! Username should contain only letters, digits, underscores and hyphens, and should be 3-16 characters long.');
return false;
}
if (!passwordRegex.test(password)) {
alert('Invalid password! Password should contain only letters, digits, underscores and hyphens, and should be 6-18 characters long.');
return false;
}
if (password !== confirmPassword) {
alert('Passwords do not match!');
return false;
}
return true;
}
// 绑定事件
loginButton.addEventListener('click', function(event) {
if (!validateLoginForm()) {
event.preventDefault();
}
});
registerButton.addEventListener('click', function(event) {
if (!validateRegisterForm()) {
event.preventDefault();
}
});
registerLink.addEventListener('click', function(event) {
event.preventDefault();
showRegisterPage();
});
loginLink.addEventListener('click', function(event) {
event.preventDefault();
showLoginPage();
});
// 初始显示登录页面
showLoginPage();
```
注意,上面的代码只是一个简单的示例,实际情况下需要更完善的验证和安全措施。
使用html,css,和javascript数组,基础函数,对象,正则表达式相关知识写关于餐饮网页
好的,我可以给你提供一个完整的餐饮网页的代码示例,包括登录注册页、首页、列表页和详情页。
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>餐饮网页</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">菜单</a></li>
<li><a href="#">关于我们</a></li>
<li><a href="#">联系我们</a></li>
<li><a href="#">登录</a></li>
</ul>
</nav>
</header>
<main>
<section id="login">
<h2>登录</h2>
<form>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required><br>
<input type="submit" value="登录">
</form>
<p>还没有账号?<a href="#register">点击注册</a></p>
</section>
<section id="register">
<h2>注册</h2>
<form>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required><br>
<label for="confirm_password">确认密码:</label>
<input type="password" id="confirm_password" name="confirm_password" required><br>
<input type="submit" value="注册">
</form>
<p>已有账号?<a href="#login">点击登录</a></p>
</section>
<section id="home">
<h2>欢迎来到餐饮网站</h2>
<div class="image-container">
<img src="images/food1.jpg">
<img src="images/food2.jpg">
<img src="images/food3.jpg">
</div>
<p>这里有各种美食等你来品尝,快来看看吧!</p>
</section>
<section id="menu">
<h2>菜单</h2>
<table>
<thead>
<tr>
<th>名称</th>
<th>价格</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>红烧肉</td>
<td>30元</td>
<td>传统的家常菜,口味浓郁。</td>
</tr>
<tr>
<td>麻婆豆腐</td>
<td>20元</td>
<td>麻辣鲜香,回味无穷。</td>
</tr>
<tr>
<td>水煮鱼</td>
<td>40元</td>
<td>鲜嫩可口,汤汁浓郁。</td>
</tr>
</tbody>
</table>
</section>
<section id="details">
<h2>红烧肉</h2>
<div class="dish-container">
<img src="images/food1.jpg">
<p>价格:30元</p>
<p>描述:传统的家常菜,口味浓郁。</p>
</div>
</section>
</main>
<footer>
<p>版权所有 © 2021 餐饮网页</p>
</footer>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 16px;
line-height: 1.5;
color: #333;
}
header {
background-color: #333;
color: #fff;
padding: 10px;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
justify-content: flex-end;
}
nav li {
margin: 0 10px;
}
nav a {
color: #fff;
text-decoration: none;
}
main {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
section {
margin-bottom: 20px;
}
h2 {
font-size: 24px;
margin-bottom: 10px;
}
form label {
display: block;
margin-bottom: 5px;
}
form input[type="text"],
form input[type="password"] {
width: 100%;
padding: 5px;
margin-bottom: 10px;
border: none;
border-radius: 5px;
}
form input[type="submit"] {
background-color: #333;
color: #fff;
padding: 5px 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}
a {
color: #333;
text-decoration: none;
}
.image-container {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
.image-container img {
width: 30%;
border-radius: 5px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 10px;
}
th, td {
padding: 5px;
text-align: left;
border: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
.dish-container {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
.dish-container img {
width: 50%;
border-radius: 5px;
}
@media (max-width: 768px) {
nav ul {
flex-direction: column;
align-items: center;
}
nav li {
margin: 10px 0;
}
.image-container {
flex-direction: column;
}
.image-container img {
width: 100%;
margin-bottom: 10px;
}
.dish-container {
flex-direction: column;
}
.dish-container img {
width: 100%;
margin-bottom: 10px;
}
}
```
JavaScript代码:
```javascript
// 在这里编写JavaScript代码
// 可以使用数组、基础函数、对象、正则表达式等相关知识来实现一些交互效果或数据处理等功能
// 比如表单验证、图片轮播、数据存储等功能
```
在以上的代码中,我使用了HTML和CSS来创建了一个简单的餐饮网页,并在JavaScript代码中留下了一些空白来让你自己实现一些交互效果或数据处理等功能。你可以使用数组、基础函数、对象、正则表达式等相关知识来完成这些功能。同时,你也可以使用一些库或框架来加速开发,比如jQuery、React等。
阅读全文