I'm trying to do the following:
- Separate sentence on white space (spaces, tabs and newlines) and save it to an array
- This array should duplicate any word followed by any punctuation mark (meaning remove the punctuation mark and replace it with the word before it and it should only remove the last punctuation mark if there are multiple in a row)
for example:
arr1 = ["first" , "second," , "third"]
newArr = ["first" , "second" , "second" , "third"]
there is the code:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p id="demo"></p>
<script>
var txt = "this is my text, that i want to fix";
var result = txt.split(/[ \t\n]+/);
var word = result.forEach(RemoveComma);
document.getElementById("demo").innerHTML = word;
function RemoveComma(value) {
var words=[];
var texts=' ';
if(value.endsWith(".*\\p{Punct}"))
{
texts= value.replace(.*\\p{Punct}, "");
words.push(texts);
words.push(texts);
}
else
words.push(value);
}
return words;
}
</script>
</body>
</html>