Javascript 如何在特定字符串之后匹配模式?

Javascript 如何在特定字符串之后匹配模式?,javascript,regex,Javascript,Regex,如何在SomeText 假设我想找到电子邮件地址,那么我应该只得到: abcd@xy.com cdf@errf.com 但是我不应该使用javascript中的正则表达式来获取上面写的电子邮件 我有一个文本文件,类似这样的东西: 在理论计算机科学和形式语言理论中,一个规则 表达式(有时称为有理表达式)[1][2]是一个 定义搜索模式的字符序列,主要用于 带字符串的模式匹配,或字符串匹配,即“查找和 替换“类”操作。这个概念出现在20世纪50年代,当时 美国人abc@cd.com数学家斯蒂芬·克莱

如何在
SomeText

假设我想找到电子邮件地址,那么我应该只得到:

abcd@xy.com

cdf@errf.com

但是我不应该使用javascript中的正则表达式来获取上面写的电子邮件

我有一个文本文件,类似这样的东西:

在理论计算机科学和形式语言理论中,一个规则 表达式(有时称为有理表达式)[1][2]是一个 定义搜索模式的字符序列,主要用于 带字符串的模式匹配,或字符串匹配,即“查找和 替换“类”操作。这个概念出现在20世纪50年代,当时 美国人abc@cd.com数学家斯蒂芬·克莱恩(Stephen Kleene)将 一种常规语言的描述,并与 Unix文本处理实用程序ed(一个编辑器)和grep(一个过滤器)

bfb@dgf.com

一些文字

姓名1/职业1/州1

abcd@xy.com

regexp在计算中非常有用,因此需要指定各种系统 regexp已经发展为提供基本的和扩展的 语法和句法;现代regexp大大增强了标准。 Regexp处理器可以在多个搜索引擎中找到,搜索引擎和 替换多个文字处理器和文本编辑器的对话框,以及 文本处理实用程序(如sed和AWK)的命令行

姓名2/职业2/州2

cdf@errf.com


您可以使用回调来替换:

var emails=[];

s.replace(/\bSomeText([\s\S]+)$/, function($0, $1) {
   $1.match(/[^\s@]+@\S+/g).map(function(e){ emails.push(e) });
   return $0;
})

console.log(emails);
// ["abcd@xy.com", "cdf@errf.com"]

PS:Regex查找电子邮件地址
[^\s@]+@\s+
在这里非常基本,电子邮件地址可能非常复杂。

您可以使用
用回调替换

var emails=[];

s.replace(/\bSomeText([\s\S]+)$/, function($0, $1) {
   $1.match(/[^\s@]+@\S+/g).map(function(e){ emails.push(e) });
   return $0;
})

console.log(emails);
// ["abcd@xy.com", "cdf@errf.com"]
PS:Regex查找电子邮件地址
[^\s@]+@\s+
在这里是非常基本的,电子邮件地址可能非常复杂。

您的解决方案:

var string   = '\nIn theoretical computer science and formal language theory, a regular expression (sometimes called a rational expression)[1][2] is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations. The concept arose in the 1950s, when the American abc@cd.com mathematician Stephen Kleene formalized the description of a regular language, and came into common use with the Unix text processing utilities ed, an editor, and grep, a filter.\n\nbfb@dgf.com\n\nSomeText\n\nname1/occupation1/state1\n\nabcd@xy.com\n\nRegexps are so useful in computing that the various systems to specify regexps have evolved to provide both a basic and extended standard for the grammar and syntax; modern regexps heavily augment the standard. Regexp processors are found in several search engines, search and replace dialogs of several word processors and text editors, and in the command lines of text processing utilities, such as sed and AWK.\n\nname2/occupation2/state2\n\ncdf@errf.com';
var someText = 'SomeText';
var regExp   = new RegExp('\\S+@\\S+\\.\\S+','g');
var emails   = string.split(someText)[1].match(regExp);
console.log(emails);
// ["abcd@xy.com", "cdf@errf.com"]
不要忘记使用
RegExp
搜索电子邮件。我提供了最简单的示例。

您的解决方案:

var string   = '\nIn theoretical computer science and formal language theory, a regular expression (sometimes called a rational expression)[1][2] is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations. The concept arose in the 1950s, when the American abc@cd.com mathematician Stephen Kleene formalized the description of a regular language, and came into common use with the Unix text processing utilities ed, an editor, and grep, a filter.\n\nbfb@dgf.com\n\nSomeText\n\nname1/occupation1/state1\n\nabcd@xy.com\n\nRegexps are so useful in computing that the various systems to specify regexps have evolved to provide both a basic and extended standard for the grammar and syntax; modern regexps heavily augment the standard. Regexp processors are found in several search engines, search and replace dialogs of several word processors and text editors, and in the command lines of text processing utilities, such as sed and AWK.\n\nname2/occupation2/state2\n\ncdf@errf.com';
var someText = 'SomeText';
var regExp   = new RegExp('\\S+@\\S+\\.\\S+','g');
var emails   = string.split(someText)[1].match(regExp);
console.log(emails);
// ["abcd@xy.com", "cdf@errf.com"]

不要忘记使用
RegExp
搜索电子邮件。我提供了一个最简单的例子。

我还没有找到一种方法在“SomeText”之后同时获得两个电子邮件地址,所以这是我的建议

去掉关键字前的所有文字。然后使用一个更简单的正则表达式作为电子邮件地址。下面的正则表达式是来自的“官方”正则表达式,但类似于“([\w\d]+@\w+.\w+)”的正则表达式可以很好地工作,并且更容易理解:)


我还没有找到在“SomeText”之后同时获得两个电子邮件地址的方法,所以这是我的建议

去掉关键字前的所有文字。然后使用一个更简单的正则表达式作为电子邮件地址。下面的正则表达式是来自的“官方”正则表达式,但类似于“([\w\d]+@\w+.\w+)”的正则表达式可以很好地工作,并且更容易理解:)


你可以像下面这样做

    var str='your text form which you need to find the email ids';

    str=str.replace(/\r\n/g,'##') // need to get all the text in one line otherwise your backrefernce will not work.

    str=str.replace(/.*sometext(.*)/i,"$1") // remove text before sometext

    str.match(/[A-Za-z0-9]+@[A-Za-z]+\.[A-Za-z]+/g)

你可以像下面这样做

    var str='your text form which you need to find the email ids';

    str=str.replace(/\r\n/g,'##') // need to get all the text in one line otherwise your backrefernce will not work.

    str=str.replace(/.*sometext(.*)/i,"$1") // remove text before sometext

    str.match(/[A-Za-z0-9]+@[A-Za-z]+\.[A-Za-z]+/g)

提示:但是如何在一些文本之后获得所有结果?请解释使用indexOf和substring(或split)在sometext之后获取文本,然后匹配您需要的内容。@Gopalkrishnasudhanshu——您需要更具体地说明这两个问题(“sometext”的特殊之处,而下面的文本“name1/职业1…”被忽略)关于您已经尝试过的内容。SomeText是一个特定的文本,类似于子标题。我有这么多匹配下面和上面的一些文字。但是我只对SomeText下面匹配的电子邮件感兴趣。@MalvolioHint:capturing groups但是如何在SomeText之后获得所有结果呢?请解释使用indexOf和substring(或split)在sometext之后获取文本,然后匹配您需要的内容。@Gopalkrishnasudhanshu——您需要更具体地说明这两个问题(“sometext”的特殊之处,而下面的文本“name1/职业1…”被忽略)关于您已经尝试过的内容。SomeText是一个特定的文本,类似于子标题。我有这么多匹配下面和上面的一些文字。但我只感兴趣的是SomeText下面匹配的电子邮件。@MalvolioIt比@anubhava solution快约2倍。比@anubhava solution快约2倍。