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

A question about regular expressions

WBOY
Release: 2016-08-04 08:53:58
Original
944 people have browsed it

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>
Copy after login

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>
Copy after login

Works perfectly

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template