简体   繁体   English

正则表达式一句话就能匹配一切

[英]Regexp match everything after a word

I am trying to extract the base64 string from a data-url. 我正在尝试从数据URL中提取base64字符串。 The string looks like this, so I am trying to extract everything after the word base64 字符串看起来像这样,所以我试图提取单词base64之后的所有内容

test = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB'

So I want to extract the following from the above string 所以我想从上面的字符串中提取以下内容

,/9j/4AAQSkZJRgABAQAAAQAB

Here is my regex 这是我的正则表达式

const base64rgx = new RegExp('(?<=base64)(?s)(.*$)');
console.log(test.match(base64rgx))

But this fails with the error: 但这失败并显示以下错误:

 VM3616:1 Uncaught SyntaxError: Invalid regular expression: /(?<=base64)(?s)(.*$)/: Invalid group

It appears that lookbehinds are not being supported. 似乎不支持向后看。 But the good news is that you don't need a lookaround here, the following pattern should work: 但是好消息是您不需要在这里四处看看,以下模式应该可以工作:

 test = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB' var regex = /.*?base64(.*?)/g; var match = regex.exec(test); console.log(match[1]); 

I'm not sure precisely how much of the string after base64 you want to capture. 我不确定到底要捕获base64之后的字符串多少。 If, for example, you don't want the comma, or the 9j portion, then we can easily modify the pattern to handle that. 例如,如果您不希望使用逗号或9j部分,那么我们可以轻松地修改模式以进行处理。

 var test = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB'; var regex = /(?<=base64).+/; var r = test.match(regex); console.log(r); 

Here's the regex: https://regex101.com/r/uzyu0a/1 这是正则表达式: https : //regex101.com/r/uzyu0a/1

You may not need regular expression: just find base64 string with indexOf and use substr 您可能不需要正则表达式:只需找到带有indexOf base64字符串并使用substr

 var test = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB'; var data = test.substr(test.indexOf('base64')+6); // 6 is length of "base64" string console.log(data); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM