Home > Web Front-end > JS Tutorial > body text

What does $ mean in js

下次还敢
Release: 2024-05-01 03:39:15
Original
606 people have browsed it

在 JavaScript 中,$ 符号是一个通配符,在正则表达式中表示:匹配任何字符(除非换行符);作为行尾锚定,限制匹配范围;配合方括号表示单词边界。

What does $ mean in js

JavaScript 中的 $

在 JavaScript 中,$ 符号是一个通配符,它可以匹配一个或多个字符。在正则表达式中使用时,它具有以下含义:

匹配任何字符

  • 单个美元符号 ($) 匹配任何字符,除了换行符 (\n) 之外。例如:
<code>/.$/g</code>
Copy after login

这个正则表达式将匹配字符串末尾的任何字符。

行尾锚定

  • 美元符号 ($) 还可以用作行尾锚定,它会将匹配范围限制在字符串的末尾。例如:
<code>/foo$/</code>
Copy after login

这个正则表达式将只匹配以 "foo" 结尾的字符串。

边界匹配

  • 当与方括号 ([ 和 ]) 结合使用时,美元符号 ($) 可以表示单词边界。例如:
<code>/\bthe\b/</code>
Copy after login

这个正则表达式将匹配 "the" 一词,即使它出现在其他单词中。

注意事项

  • $ 符号在 JavaScript 中是一个特殊字符,因此在正则表达式中使用它时,必须转义。例如:
<code>var re = /\$\d+/;  // 正确</code>
Copy after login
  • $ 符号还可以在字符串中用作转义字符。例如:
<code>var str = "This is a $10 item.";</code>
Copy after login

其他用法

除了正则表达式之外,$ 符号在 jQuery 库中也有许多其他用法,例如:

  • 查找元素:$(selector)
  • 触发事件:$(element).trigger(event)
  • 操作 DOM:$(element).html()

The above is the detailed content of What does $ mean in js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!