迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中标记字符串

作者:迹忆客 最近更新:2023/03/09 浏览次数:

JavaScript 令牌更像是一个单独扫描字符、表达式和字符串的可视化概念。

如果我们假设将 10+5 作为表达式,那么 lexer(将每个有效字符区分为标记的过程)会将 10 定义为 Number 类型的标记,+ 定义为 Plus,和 5 作为数字类型。

在所有字符都被标记化,更具体地分类之后,它们将被发送到解析。然后解析器规则将指定标记来定义表达式。

我们将遵循词法分析器和解析器规则来定义以下示例中的每个单词。全文将首先被扫描为按空格区分的单个单词。

然后,整个标记化组将被解析。这个概念为拆分字符串或任何表达式提供了逐步的方法。

抽象语法树执行表达式的可视化。让我们深入代码以获得示范性解释。

var text = "Is not it weird to live in a world like this? It is a 42";
var words = text.toLowerCase();
var okay = words.split(/\W+/).filter(function(token) {
    return token.length == 2;
});
console.log(okay);

输出:

使用 split() 方法对字符串进行标记

因此,text 字符串被转换为小写,然后 split() 方法完成了分词的任务。

程序是抽象的,因此我们无法直观地确定内部工作流程。我们已经从标记中过滤掉了一些特定长度的单词。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便