# 特殊字符

特殊字符，指在正则中有特殊含义的字符

## 特殊字符集

> 若要匹配特殊字符，必须首先使字符"转义"，即，将反斜杠字符 放在它们前面

| 特别字符 | 描述                                                                                              |                  |
| ---- | ----------------------------------------------------------------------------------------------- | ---------------- |
| $    | <ul><li>匹配输入字符串的结尾位置</li><li>若设置了正则对象的m属性，则 $ 也匹配 '\n' 或 '\r'</li><li>要匹配 $ 字符本身，使用 $</li></ul> |                  |
| ( )  | <ul><li>标记一个子表达式的开始和结束位置</li><li>要匹配这些字符，使用 ( 和 )</li></ul>                                     |                  |
| \*   | <ul><li>匹配前面的子表达式零次或多次</li><li>要匹配 \* 字符，使用 \*</li></ul>                                        |                  |
| +    | <ul><li>匹配前面的子表达式一次或多次</li><li>要匹配 + 字符，使用 +</li></ul>                                          |                  |
| .    | <ul><li>匹配除换行符 \n 之外的任何单字符</li><li>要匹配 . ，使用 . </li></ul>                                       |                  |
| \[   | <ul><li>标记一个中括号表达式的开始</li><li>要匹配 \[，使用 \[</li></ul>                                            |                  |
| ?    | <ul><li>匹配前面的子表达式零次或一次</li><li>或指明一个非贪婪限定符</li><li>要匹配 ? 字符，使用 ?</li></ul>                      |                  |
| \\   | <ul><li>将下一个字符标记为或特殊字符</li><li>或原义字符</li><li>或向后引用</li><li>或八进制转义符</li></ul>                    |                  |
| ^    | <ul><li>匹配输入字符串的开始位置</li><li>在方括号中使用，表示不接受该字符集合</li><li>要匹配 ^ 字符本身，使用 ^</li></ul>               |                  |
| {    | <ul><li>标记限定符表达式的开始</li><li>要匹配 {，使用 {</li></ul>                                                |                  |
| \|   | <ul><li>指明两项之间匹配其中一个</li><li>要匹配                                                                | ，使用 \|</li></ul> |

## 特殊字符模式

### 无转义符

当正则模式要匹配”特殊字符“时，必须在”特殊字符“前加反斜杠"\\"，否则会报错；如下述，正则表达式中有特殊字符”\[“，未使用\进行转义，会报SyntaxError

```javascript
/abc[123/.exec('abc[123'); // 报错 SyntaxError

/** Uncaught SyntaxError: Invalid regular expression: missing **/
```

### 转义符使用

当正则模式要匹配”特殊字符“时，在”特殊字符“前加反斜杠"\\"，则可正常匹配该“特殊字符”

```javascript
/abc\[123/.exec('abc[123'); // 匹配
/** ["abc[123", index: 0, input: "abc[123", groups: undefined] **/
```

![](/files/-LivPLNHK6OHT9BJwQSO)

## 斜杠

### 正斜杠"\\"

正斜杠常见用途汇总

| 用途             | 示例                                                                                                                                                               |
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 操作系统路径分隔符      | <ul><li>window系统：C:/Windows/addins</li><li>Unitx系统：/usr/local/services</li></ul>                                                                                 |
| Url地址标记符与路径分割符 | <p>如：<https://www.qq.com/ch/photo></p><ul><li>地址标记符：https:<strong>//</strong></li><li>路径分隔符：[www.qq.com](http://www.qq.com)<strong>/ch/photo/</strong></li></ul> |
| 计算符号除÷         | 在编程中2÷4表示为&#x32;**/**&#x34;                                                                                                                                      |
| 代码注释//         | <p>在C++/C#/Java/JavaScript/PHP/JS等主流编程语言中</p><p>双//表示代码注释</p>                                                                                                    |
| 日期分隔符          | 如2019年7月2号可表示为07/02/2019                                                                                                                                         |
| 文章里表示“或”，“和”   | 我掌握C++/C#/Java/JavaScript/PHP/JS等语言                                                                                                                              |

### 反斜杠“\”

| 用途        | 示列                                                                                                                                                         |                              |
| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- |
| 操作系统路径分隔符 | <ul><li>window系统：C:\Windows\addins</li></ul>                                                                                                               |                              |
| 转义符       | <p> 转义字符放在一个字符序列中时，可对它后续几个字符采取替代解释</p><ul><li>回车：\r</li><li>换行：\n</li><li>换页：\f</li></ul>                                                                  |                              |
| 强制换行符     | <p>在linux中，反斜线常被放在一行的末尾用来指示编译器忽略其后的换行符，</p><p>使得下一行会被当做同一行语句，将代码拆解成“连续行”</p><p></p><p>如下，在linux中查看nginx进程的命令行：</p><p><code>$ ps -axu \</code></p><p><code> | grep nginx</code></p><p></p> |

### 多个转义符使用含义

```javascript
console.log('\'); // 报错，反斜杠把最后一个引号进行转义，导致第一个引号无配对的结束引号
/** Uncaught SyntaxError: Invalid or unexpected token **/

console.log('\\') // 正确，第一个反斜杠对第二个反斜杠进行转义，输出一个反斜杠
/** / **/

console.log('\\\'); // 报错，第一个转义第二个反斜杠，第三个反斜杠转义最后一个单引号
/** Uncaught SyntaxError: Invalid or unexpected token **/

console.log('\\\\'); // 正确，第一个和第三个反斜杠分别转义紧跟在后面的反斜杠
/** \\ **/
```

```javascript
const regexp = new RegExp('\\.', 'g');
console.log(regexp); // /\./g
console.log(regexp.test('.')); // true
console.log(regexp.test('\.')); // false
console.log(regexp.test('\a')); // false
console.log(regexp.test('.a')); // true
console.log(regexp.test('n.a')); // true
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nxtech.gitbook.io/regexp/grammar/special-characters.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
