var a = "[i] earned [c] coin for [b] bonus";
How to get the string "__ earned __ coins for __ bonus" from the above variable in JavaScript?
All I want to do is replace all the brackets [] and its contents with __ .
[]
__
a = a.replace(/\[.*?\]/g, '__');
If you expect new lines to appear, you can use:
a = a.replace(/\[[^\]]*?\]/g, '__');
a = a.replace(/\[[^\]]+\]/g, '__');
jsFiddle .
Here's a fun example of matching groups.
var text = "[i] italic [u] underline [b] bold"; document.body.innerHTML = text.replace(/\[([^\]]+)\]/g, '(<$1>$1</$1>)');
/ // BEGIN pattern \[ // FIND left bracket (literal) '[' ( // BEGIN capture group 1 [ // BEGIN character class ^ // MATCH start anchor OR \] // MATCH right bracket (literal) ']' ] // END character class + // REPEAT 1 or more ) // END capture group 1 \] // MATCH right bracket (literal) ']' / // END pattern g // FLAG global search
Source: https://habr.com/ru/post/888889/More articles:Sql Server - custom CTE in a subquery - sql-serverxstream - reuse default converter in user converter - javaHow to remove south from django project - pythonJQuery or CSS? ... To scale a div - jqueryHTML as a result for calling AJAX (PROs an CON) - javascriptAndroid: saving a file permanently (even after clear data / deletion) - androidUnderstanding Linux scheduling using pthreads - linuxXSD: multiple types with the same element name - typesDebugging a strange error, which depends on the selected scheduler - c ++pthread_mutex_lock lock, but no owner installed - cAll Articles