⊗jsSpREVr 116 of 294 menu

Variable with a regex in JavaScript

Regular expressions can be placed into variables. Let's look at an example. Let's say we have the following string:

let str = 'aaa bbb ccc';

Let's say we have the following code with a regex:

let res = str.replace(/\w+/, '!');

Let's move the regular expression into a variable:

let reg = /\w+/; let res = str.replace(reg, '!');

Move the regex into a variable in the following code:

let str = 'a aa aaa aaaa aaaa'; let res = str.replace(/a+/, '!');
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline