Javascript的split函数中的正则表达式

时间:2021-10-08 01:35:40

I make ajax request to receive a text file. I need to read this text in javascript.

我发出ajax请求来接收文本文件。我需要在javascript中阅读此文本。

The Text file is like this:

Text文件是这样的:


kjnggnfgnfkjg

kjnggnfgnfkjg

as "bob"

作为“鲍勃”

bs "alice"

bs“爱丽丝”

fmdklfmdlkfk

fmdklfmdlkfk

as "Marc"

作为“马克”

bs "Julien"

bs“朱利安”

dfdlf ..

dfdlf ..


I need to create two array, one with the line that begin with 'as' and the second with the line that begin with 'bs' word. Every element of array doesn't need to have the 'as' or 'bs' word as well as "". in this case:

我需要创建两个数组,一个是以'as'开头的行,另一个是以'bs'开头的行。数组的每个元素都不需要“as”或“bs”字以及“”。在这种情况下:

a=[bob,Marc] and b=[alice,julien]

Can you help me please?

你能帮我吗?

1 个解决方案

#1


1  

Once you have split the text in lines, you can filter and map those :

将文本拆分为行后,可以对这些文本进行过滤和映射:

var lines = t.split('\n');
var asPattern = /^as \"(\w+)\"$/;
var bsPattern = /^bs \"(\w+)\"$/;
a = lines.filter(l => l.match(asPattern))
         .map(l => l.replace(asPattern, "$1"));
b = lines.filter(l => l.match(bsPattern))
         .map(l => l.replace(bsPattern, "$1"));

list.filter(predicate) will return a sublist of the items matching the predicate. Here we use a lambda function as the predicate, which will return true only if the line match the pattern.

list.filter(谓词)将返回与谓词匹配的项的子列表。这里我们使用lambda函数作为谓词,只有当行与模式匹配时才会返回true。

>lines
["kjnggnfgnfkjg", "", "as "bob"", "", "bs "alice"", "", "fmdklfmdlkfk", "", "as "Marc"", "", "bs "Julien"", "", "dfdlf"]
>lines.filter(l => l.match(/^as \"(\w+)\"$/))
["as "bob"", "as "Marc""]

list.map(transformationFunction) will return a list of the results of the application of a transformation function to each element of the initial list. Here we reuse the same pattern and use its first group as a replacement.

list.map(transformationFunction)将返回转换函数的应用结果列表到初始列表的每个元素。在这里,我们重用相同的模式,并使用其第一个组作为替代。

>filteredLines
["as "bob"", "as "Marc""]
>filteredLines.map(l => l.replace(/^as \"(\w+)\"$/, "$1"))
["bob", "Marc"]

#1


1  

Once you have split the text in lines, you can filter and map those :

将文本拆分为行后,可以对这些文本进行过滤和映射:

var lines = t.split('\n');
var asPattern = /^as \"(\w+)\"$/;
var bsPattern = /^bs \"(\w+)\"$/;
a = lines.filter(l => l.match(asPattern))
         .map(l => l.replace(asPattern, "$1"));
b = lines.filter(l => l.match(bsPattern))
         .map(l => l.replace(bsPattern, "$1"));

list.filter(predicate) will return a sublist of the items matching the predicate. Here we use a lambda function as the predicate, which will return true only if the line match the pattern.

list.filter(谓词)将返回与谓词匹配的项的子列表。这里我们使用lambda函数作为谓词,只有当行与模式匹配时才会返回true。

>lines
["kjnggnfgnfkjg", "", "as "bob"", "", "bs "alice"", "", "fmdklfmdlkfk", "", "as "Marc"", "", "bs "Julien"", "", "dfdlf"]
>lines.filter(l => l.match(/^as \"(\w+)\"$/))
["as "bob"", "as "Marc""]

list.map(transformationFunction) will return a list of the results of the application of a transformation function to each element of the initial list. Here we reuse the same pattern and use its first group as a replacement.

list.map(transformationFunction)将返回转换函数的应用结果列表到初始列表的每个元素。在这里,我们重用相同的模式,并使用其第一个组作为替代。

>filteredLines
["as "bob"", "as "Marc""]
>filteredLines.map(l => l.replace(/^as \"(\w+)\"$/, "$1"))
["bob", "Marc"]