正则表达式:如何删除未连接到图形的所有字符(特定字符旁边)

时间:2017-11-04 22:26:02

标签: javascript jquery arrays regex string

我有一个返回如下结果的函数:given output

我的给定字符串可以与此示例进行比较:

  

var string ='111 1 1 \ n 1111111 1 \ n 11111 11 11'

好的,你看:我的字符串中包含一些'1'空格新行字符。

现在我想得到像这样的正则表达式结果(删除所有带红色圆圈的字符):result output

正则表达式应删除所有'1'字符 未连接 big '1'字符行

(对不起,我知道这听起来很疯狂,但我确实需要这个结果:p)


例:
var string=' 111 1111111111111111 11 1'

var result='1111111111111111' 

(删除所有'1'/'' - 未直接连接到2个空格字符之间的最大字符串的组 - ['1111111111111111'])

我希望你能明白我的意思:)。

提前致谢,乔纳森

PS:Textoutput:pastbin.com

2 个答案:

答案 0 :(得分:2)

我认为,其中一种方法是使用\ b

 '    111   1  '.replace(/(\b1{0,7}\b)|( +)/gi, '')

答案 1 :(得分:0)

您可以尝试像\d{8,}这样简单的事情。此正则表达式将帮助您将任何数字\d与其8位或更多数字的条件进行匹配。

演示:https://regex101.com/r/bvhMDL/1/

实施例



var string = ' 111  1111111111111111 11    1';
var regex = /\d{8,}/g;
var match = regex.exec(string);
document.write(match[0])




如果你的字符串有多行,这是另一个例子。



var string = `
 111 1  1111111111111111 11    1
 11 1   1\n   11111111111 11    1
 1   1\n 1  1111111111111111111 11    1
 11   1\n   111111111111 11    1
 111  1111111111111111111 11    1
 1 1   1\n   1111111111111111 11    1
 111  1111111111111111111 11    1
 11 \n 111111111111 11    1
`;
var regex = /\d{8,}/g;
var match = regex.exec(string);
while (match != null) {
  document.write(match[0]+'<br>')
  match = regex.exec(string);
}
&#13;
&#13;
&#13;

使用实际数据时,请确保使用1{8,}而不是\d{8,},因为您在那里输入了零。这是一个有实际日期的例子

&#13;
&#13;
var string = `
Array (134)
0 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
2 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
3 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
4 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
5 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
6 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
7 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
8 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
9 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
10 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
11 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
12 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
13 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
14 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
15 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
16 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
17 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
18 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
19 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
20 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
21 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
22 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
23 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
24 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
25 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
26 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
27 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
28 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
29 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
30 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
31 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
32 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
33 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
34 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
35 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
36 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
37 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
38 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
39 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
40 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
41 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
42 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
43 "0000000000000000000000000000000000000000000000000011100000000000000000000000000000000000000000000000"
44 "0000000000000000000000000000001100000000000000000001100000000000000000001000000000000000000000000000"
45 "0000000000000000000000000000001110000000000000000000000000000000000000011100000000000000000000000000"
46 "0000000000000000000000000000000100000000000000000000000000000000000000001000000000000000000000000000"
47 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
48 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
49 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
50 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
51 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
52 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
53 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
54 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
55 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
56 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
57 "0000000000000000000000000000000000000000111111111000000000000000000000000000000000000000000000000000"
58 "0000000000000000000000000000000000011100111111111000000000000000000000000000000000000000000000000000"
59 "0000000000000000000000000000000000001100111111111000000000000000000000000000000000000000000000000000"
60 "0000000000000000000000000000000000000000111111111000000000000000000000000000000000000000000000000000"
61 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
62 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
63 "0000000000000000000000000000000000000000111111111111111111111100000100000000000000000000000000000000"
64 "0000000000000000000000000000000000000000111111111111111111111100001110000000000000000000000000000000"
65 "0000000000000000000000000000000000000000111111111111111111111100000100000000000000000000000000000000"
66 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
67 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
68 "0000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000"
69 "0000000000000000000000000000000000000000111111110000001111111100000000000000000000000000000000000000"
70 "0000000000000000000000000000000000000000111111110000000000000000000000000000000000000000000000000000"
71 "0000000000000000000000000000000000000000111111110000000000000000000000000000000000000000000000000000"
72 "0000000000000000000000000000010000000000111111110000000000000000000000000000000000000000000000000000"
73 "0000000000000000000000000000111000000001111111111100000000000000000000000000000000000000000000000000"
74 "0000000000000000000000000000010000000001111111111111111111111100000000000000000000000000000000000000"
75 "0000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000"
76 "0000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000"
77 "0000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000"
78 "0000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000"
79 "0000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000"
80 "0000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000"
81 "0000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000"
82 "0000000000000000000000000000000000000000000000000000011111111100000000000000000000000000000000000000"
83 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
84 "0000000000000000000000000000000000000000000000000000000000000000000000011100000000000000000000000000"
85 "0000000000000000000000000001100000000000000000000000000000000000000000011100000000000000000000000000"
86 "0000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000"
87 "0000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000"
88 "0000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000"
89 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
90 "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
`;
var regex = /1{8,}/g;
var match = regex.exec(string);
while (match != null) {
  document.write(match[0]+'<br>')
  match = regex.exec(string);
}
&#13;
&#13;
&#13;