I'm starting with JavaScript and I'm starting to like it because of the features it has.
I meet this problem
I have a text string with the following "1. I LIKE TO USE [ JAVASCRIPT ] VERSION 1.1 "
What I want, keep the word JAVASCRIPT without the brackets
With this I get the word together with the brackets, that is [ JAVASCRIPT ]
<script>
var p = '1. ME GUSTA USAR [ JAVASCRIPT ] VERSION 1.1 "';
var PrinActi = /\[.*?\]/g;
var PrinAct2 = p.match(PrinActi);
console.log(PrinAct2);
</script>
Now I need to remove the brackets in the same code I am using replace for, but it does not work for me.
<script>
var p = '1. ME GUSTA USAR [ JAVASCRIPT ] VERSION 1.1 "';
var PrinActi = /\[.*?\]/g;
var PrinAct2 = p.match(PrinActi);
var PrinAct3 = PrinAct2.replace('[', '').replace(']', '');;
console.log(PrinAct3);
</script>
The idea of all this is to get what is in the brackets and then remove the brackets, to simply get the word JavaScript I can use this:
var PrinActi = /\bJAVASCRIPT/;
And it's not what I want.
I really appreciate any help you can give me.
I remind you that the documentation is always our best friend, if we read the documentation of
match
you will realize that a is returnedarray
with the results... If the result is unique you can access this unique element by doing:The error you get:
It happens because the method
replace()
is part of an object of typestring
and not an objectarray
like the one returned by the functionmatch()
, so the method cannot be found.