通过js正则匹配一个URL,以http或https开头,并以.com结尾。
时间: 2023-07-21 17:31:10 浏览: 120
可以使用以下正则表达式来匹配一个以http或https开头,以.com结尾的URL:
```
/^(http|https):\/\/[^\s]*\.com$/
```
其中,^表示匹配字符串的开始,$表示匹配字符串的结尾。括号中的“http|https”表示匹配http或https这两个字符串中的任意一个。双斜线“\/\/”表示匹配一个斜线字符。[^\s]*表示匹配任意数量的非空白字符,\.表示匹配一个点字符,com表示匹配com字符串。
相关问题
js正则匹配以xxx开头,以xxx结尾
可以使用正则表达式的 ^ 和 $ 符号来匹配以 xxx 开头和以 xxx 结尾的字符串,示例代码如下:
```javascript
const str = "xxx中间的内容xxx";
const reg = /^xxx.*xxx$/;
console.log(reg.test(str)); // 输出 true
```
解析:
- ^xxx 表示字符串以 xxx 开头。
- .* 表示匹配任意字符 0 次或多次,这里的意思是匹配字符串中 xxx 开头和结尾之间的任意字符。
- xxx$ 表示字符串以 xxx 结尾。
因此,/^xxx.*xxx$/ 的含义就是匹配以 xxx 开头,以 xxx 结尾,中间可以是任意字符的字符串。
js正则匹配以{开头,以}结尾
可以使用正则表达式`/{.*?}/`来匹配以`{`开头,以`}`结尾的字符串。
示例代码:
```javascript
const str = '这是一个{测试}字符串,{hello}world!';
const reg = /{.*?}/g;
const matches = str.match(reg);
console.log(matches); // ["{测试}", "{hello}"]
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)