Home >Backend Development >PHP Tutorial >A question about regular expressions

A question about regular expressions

WBOY
WBOYOriginal
2016-08-04 08:53:58944browse

Question: There is a string: "python php ruby ​​javascript jsonp perhapsphpisoutdated"
For this string, use pure regular expression to get all words with p but not ph

Output array [ 'python', 'javascript', 'jsonp' ]

I have been thinking about this problem for a long time and I have no idea
My solution is

<code>var result = str.match(/\b\w*(?=p)\w*\b/g)
                .filter((value)=>!/.*(?=ph)/.test(value))
var result2 = str.match(  /\b((?!ph|\s).)*((p[^h\s]((?!ph|\s).)*)|p)\b/g  ) 
console.log(result2)</code>

But it does not meet the requirements of pure regularity

A big guy in the group gave this answer

<code>/\b((?!ph|\s).)*((p[^h\s]((?!ph|\s).)*)|p)\b/g </code>

Works perfectly

But I can’t understand it, I hope someone can help me understand it

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn