I have a text and after deleting special characters (!@#$%^&*()-= `";:'><.?/) and show just letters and numbers (and float numbers like 23.4 ) it returns some extra space
const input : 'this is a signal , entry : 24.30 and side is short';
const text = input.replace(/\.(?!\d)|[^\w.]/g, " ").toUpperCase();
console.log(text.split(" "))
the output :
[
'THIS', 'IS', 'A',
'SIGNAL', '', '',
'', 'ENTRY', '',
'', '24.30', 'AND',
'SIDE', 'IS', 'SHORT'
]
but I want to be this :
[
'THIS', 'IS', 'A',
'SIGNAL', 'ENTRY', '24.30',
'AND', 'SIDE', 'IS',
'SHORT'
]
And when I replace spaces and enters with empty string , returns this :
[ 'THISISASIGNALENTRY24.30ANDSIDEISSHORT' ]
what is the problem of my code?
CodePudding user response:
Instead of replacing, consider matching all the sorts of characters you want to produce the array of words. It looks like you want something like:
const input = 'this is a signal , entry : 24.30 and side is short';
const matches = input.toUpperCase().match(/[\w.] /g);
console.log(matches);
CodePudding user response:
The second parameter in the replace
method needs to be an empty string and not a space as you have it in your code.
Just do:
...
const text = input.replace(/\.(?!\d)|[^\w.]/g, "").toUpperCase();
...