img:const { images } = new DOMParser().parseFromString(html, 'text/html');
// или
const images = document
.createRange()
.createContextualFragment(html)
.querySelectorAll('img');
// или
const images =
(el => (el.innerHTML = html, el.getElementsByTagName('img')))
(document.createElement('div'));img извлечь значения src и srcset:const getSrc = img => [ img.src, img.srcset ];
// или
const getSrc = img => [ 'src', 'srcset' ].map(n => img.getAttribute(n));
// или
const getSrc = ({ attributes: a }) => [ a.src.value, a.srcset.value ];const result = Array.from(images, getSrc);
// или
const result = Array.prototype.map.call(images, getSrc);
// или
const result = [];
for (const n of images) {
result.push(getSrc(n));
}
// или
const result = [];
for (let i = 0; i < images.length; i++) {
result[i] = getSrc(images[i]);
}
// или
const result = (function get(i, n = images.item(i)) {
return n ? [ getSrc(n), ...get(i + 1) ] : [];
})(0);
g на самом деле отсутствует. Смотрите внимательнее, куда вы его попытались вписать.
str.match(/(?<=\. ).+(?= \(%\))/)[0]
// или
str.replace(/^.*?\. | \(%\).*$/g, '')
// или
str.split('. ').pop().split(' (%)').shift()
$arr = array_slice(explode(' ', $str), 1);.array_map(fn($n) => trim($n, '"'), $arr).
str.replace(/(\d{2})(\d{2})/, '$1.$2.')
// или
str.replace(/(?=\d{4}$|\d{6}$)/g, '.')
// или
str.match(/(..)(..)(.+)/).slice(1).join('.')
// или
[ 0, 2, 4 ].map((n, i, a) => str.slice(n, a[i + 1])).join`.`
// или
[...str].reduce((acc, n, i) => acc + n + ([ ,'.',,'.' ][i] || ''))
// или
''.concat(...Array.from(str, (n, i) => '24'.includes(i) ? `.${n}` : n))
const utm = Object.fromEntries(Array.from(
location.search.matchAll(/utm_(.+?)=([^&]*)/g),
n => n.slice(1)
));
// или
const utm = [...new URLSearchParams(location.search)].reduce((acc, n) => (
n[0].startsWith('utm_') && (acc[n[0].slice(4)] = n[1]),
acc
), {});
str.replace(/\d{3}-\d{2}/, '***-**')
// или
str.slice(0, 9) + '***-**' + str.slice(15)
// или
str.replace(/\d(?=\d*-)/g, '*')
// или
str.replace(/\d+(?=-)/g, m => '*'.repeat(m.length))
// или
str.replace(/(?<=\) ).{6}/, '***-**')
// или
str.match(/^.{9}|.{3}$/g).join('***-**')
const getSrc = img => img.getAttribute('src');
// или
const getSrc = img => img.attributes.src.value;const relativeOnly = f => img => {
const src = getSrc(img);
if (!/^https?:\/\//.test(src)) {
f(img, src);
}
};document.querySelectorAll('img').forEach(relativeOnly((img, src) =>
img.outerHTML = `
<picture>
<source srcset="${src}" type="image/svg+xml">
${img.outerHTML}
</picture>`
));const wrapImages = relativeOnly((img, src) => {
const picture = document.createElement('picture');
const source = document.createElement('source');
source.srcset = src;
source.type = 'image/svg+xml';
img.replaceWith(picture);
picture.append(source, img);
});for (const n of document.getElementsByTagName('img')) {
wrapImages(n);
}
// или
Array.prototype.forEach.call(document.images, wrapImages);
str.match(/(?<=\{{2}).*?(?=\}{2})/g) ?? []
// или
Array.from(str.matchAll(/\{\{(.*?)\}\}/g), n => n[1])
toInsert = 'test.'
newStr = str.dup.sub!(/(?<=www\.)/, toInsert) || (toInsert + str)