我想从文本区域中检索字符串,然后删除包含起始字符串的行'>' (无论是否在'>')前面都有空格,删除一个或多个' - ', 然后将字符串拆分为数组,结果如下:
[BBAAAACC,CBDAAACC,EBAAAACC,FBAAAACC]
我尝试过使用正则表达式,但无法获得结果。
var data = document.getElementById("data").value.replace(/^[>-]+/g,"");
var array = data.split("\n");
console.log(array);

<textarea rows="15" cols="50" id="data">
>1/1AA
BBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>2/1B
CBDAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>3/1-CD
EBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>4/1-11
FBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
</textarea>
&#13;
答案 0 :(得分:1)
您的代码是一个开始,您的所有数组需求都需要进行一些清理:
var array = document
.getElementById("data")
.value
.split("\n")
.map(l => l.trim() // Removes extra spaces before and after
.replace(/-/g,'')) // Removes all occurrences of '-'
.filter(l => l.length && l[0] !== ">") // Keeps only the lines with a length and not beginning with '>'
console.log(array);
&#13;
<textarea rows="15" cols="50" id="data">
>1/1AA
BBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>2/1B
CBDAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>3/1-CD
EBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>4/1-11
FBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
</textarea>
&#13;
答案 1 :(得分:1)
您最初的想法是删除以>
和任何-
符号开头的行,但您似乎只需要从每行的开头提取1个大写ASCII字母的块。< / p>
您可以使用
var data = document.getElementById("data").value.match(/^\s*[A-Z]+/mg).map(x => x.trim())
正则表达式/^\s*[A-Z]+/mg
匹配
^
- 开始行\s*
- 0+ whitespaces [A-Z]+
- 一个或多个大写ASCII字母/mg
- 多行和全局修饰符。请注意,如果您想确保匹配-
后面的字母,您可以在模式的末尾添加(?=-)
正向前瞻。
请注意,您仍需要使用\s*
(或.map(x => x.trim()
)修剪与.map(function(x) {return x.trim();})
匹配的所有主要空格。您也可以捕获字母([A-Z]+)
,然后在循环抓取RegExp#exec()
值中使用match[1]
,但这涉及更多代码。
参见JS演示:
var data = document.getElementById("data").value.match(/^\s*[A-Z]+(?=-)/mg).map(x => x.trim());
console.log(data);
<textarea rows="15" cols="50" id="data">
>1/1AA
BBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>2/1B
CBDAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>3/1-CD
EBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>4/1-11
FBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
</textarea>
答案 2 :(得分:0)
拥有地图和过滤器 - 另一个答案是ES5 / 6,因此无法在旧版浏览器中使用:
var array= document.getElementById("data").value
.split("\n")
.map(function(line) {
line = line ? line.trim() : "";
line = line.replace(/-/g, "");
return line; })
.filter(function(line) {
return line && line.indexOf(">") != 0;});
console.log(array);
<textarea rows="15" cols="50" id="data">
>1/1AA
BBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>2/1B
CBDAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>3/1-CD
EBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
>4/1-11
FBAAAACC-------------------------------------------------------------
------------------------------------------------------------------------
-------------
</textarea>