在JavaScript RegEx中匹配不可见的字符 [英] Matching invisible characters in JavaScript RegEx

查看:125
本文介绍了在JavaScript RegEx中匹配不可见的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些包含不可见字符的字符串,但它们在某些可预测的位置。通常我要提取的文本环绕,然后在第二次出现后我想保留文本的其余部分。

I've got some string that contain invisible characters, but they are in somewhat predictable places. Typically the surround the piece of text I want to extract, and then after the 2nd occurrence I want to keep the rest of the text.

我似乎无法想象如何关闭不可见的字符,将它们排除在我的结果之外。为了匹配隐形我一直在使用这个正则表达式: / \\\\\\ x00-\\\\\\\\\\\\\\\\\\\\\ c>似乎有用。

I can't seem to figure out how to both key off of the invisible characters, and exclude them from my result. To match invisibles I've been using this regex: /\xA0\x00-\x09\x0B\x0C\x0E-\x1F\x7F/ which does seem to work.

以下是一个例子: [invisibles]保持匹配1 [隐身]保持匹配2

以下是我迄今为止没有成功使用的内容:

Here's what I've been using so far without success:

/([\xA0\x00-\x09\x0B\x0C\x0E-\x1F\x7F] +)(+)([\xA0\x00-\x09 \x0B\x0C\x0E-\x1F \ x7F] +)/(。+)

我有那里的捕获组,但由于我不得不以这种方式使用正则表达式,所以它已经有一段时间了,所以我知道我错过了一些重要的东西。我希望只是让隐藏的匹配非捕获组,但似乎JavaScript不支持这一点。

I've got the capture groups in there, but it's bee a while since I've had to use regex's in this way, so I know I'm missing something important. I was hoping to just make the invisible matches non-capturing groups, but it seems that JavaScript does not support this.

推荐答案

东西这样看起来像你想要的。第二个正则表达式你几乎可以工作,但 / 完全是错误的地方。也许你没有正确读出小组数据。

Something like this seems like what you want. The second regex you have pretty much works, but the / is in totally the wrong place. Perhaps you weren't properly reading out the group data.

var s = "\x0EKeep as match 1\x0EKeep as match 2";
var r = /[\xA0\x00-\x09\x0B\x0C\x0E-\x1F\x7F]+(.+)[\xA0\x00-\x09\x0B\x0C\x0E-\x1F\x7F]+(.+)/;

var match = s.match(r);

var part1 = match[1];
var part2 = match[2];

这篇关于在JavaScript RegEx中匹配不可见的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆