Incase we need to find any number of white spaces like for following silly example
header{
background-color:red;
}
header {
color: white;
}
header {
border: 1px solid blue;
}
Here I want to replace header with another selector for example “section{“. So we want to match “header” tag, white spaces and “{” so that we only replace header selector.
This is how we do in JavaScript:
const str = `
header{
background-color:red;
}
header {
color: white;
}
header {
border: 1px solid blue;
}
`;
const pattern = /header(\s*){/gi;
const css = str.replace( pattern, `section{` );
console.log(css)
This is how we do in PHP:
<?php
$str = '
header{
background-color:red;
}
header {
color: white;
}
header {
border: 1px solid blue;
}
';
$pattern = '/header(\s*){/m';
$css = preg_replace( $pattern, 'section{', $str );
echo $css;