📝 Edit on GitHub
Text
Case
> myText.toUpperCase()
Slicing
> myText.substr(first, length)
> myText.substring(start, end)
Replace character
> 'AAA'.replace(/A/g, 'C')
'CCC'
Or the more verbose.
> 'AAA'.replace(new RegExp("A", "g"), 'C')
'CCC'
You might use RegExp
to escape characters for you.
> new RegExp("A", "g")
/A/g
> new RegExp("/A")
/\/A/
Warning
These will only replace the first occurence.
Using .replace
without regex (like you would do in Python):
> 'AAA'.replace('A', 'C')
'CAA'
Using regex without g
:
> 'AAA'.replace(/A/, 'C')
'CAA'
Remove empty lines
> myText.replace(/^\n/gm, '');
The m
modified is recommended for multi-line search
https://www.w3schools.com/jsref/jsref_regexp_m.asp
Remove empty line from start and end
> myText.replace(/^\s+|\s+$/g, '');
Note this removes all whitespace and not just \n
.
Template literals
See Template literals on Mozilla docs.