Python代码的模拟操作到JavaScript RE模块

发布于 2025-02-03 04:35:03 字数 577 浏览 3 评论 0原文

这是代码:

        lines = re.split('\\s*\\n+\\s*', text)

        joint_stack = []

        for line in lines:
            words = re.split('\\s+', line)
            instruction = words[0]

我的JS代码是:

  _parse_hierarchy(text) {

    var lines = text.split('\\s*\\n+\\s*');

    console.log("Test _parse_hierarchy  -> ", lines);

我有数组,但我在一个项目中获得了整个文字。我的数组长度是1。

看起来我需要以行划分的等级...

我也尝试var lines = text.match('\\ s*\\ n+\\ s*');>没有成功。

有建议吗?

Here is the code :

        lines = re.split('\\s*\\n+\\s*', text)

        joint_stack = []

        for line in lines:
            words = re.split('\\s+', line)
            instruction = words[0]

My JS code is :

  _parse_hierarchy(text) {

    var lines = text.split('\\s*\\n+\\s*');

    console.log("Test _parse_hierarchy  -> ", lines);

I got array but i got whole text in one item. My array length is 1.

Looks like i need regex to split by line...

I also try var lines = text.match('\\s*\\n+\\s*'); with no success.

Any suggestion ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2025-02-10 04:35:03

JS格式的正确方法:\\ => \
没有')。

    var lines = text.split(/\s*\n+\s*/);

    console.log("Test lines  -> ", lines);

   for (var key in lines) {

      var line = lines[key];
      console.log("Test _parse_hierarchy FIRST WORD  -> ", line);

      var words = line.split(/\s+/);
      // var firstword = words[0];

Correct way for js format: \\ => \
and without ' or " .

    var lines = text.split(/\s*\n+\s*/);

    console.log("Test lines  -> ", lines);

   for (var key in lines) {

      var line = lines[key];
      console.log("Test _parse_hierarchy FIRST WORD  -> ", line);

      var words = line.split(/\s+/);
      // var firstword = words[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文