- Код: Выделить всё
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<style>
</style>
</head>
<body>
<textarea id="t1" name="text1" cols="60" rows="10"></textarea>
<br />
<br />
<button>Count</button>
<script>
const tarea = document.querySelector('#t1');
document.querySelector('button').addEventListener('click', function(){
const val = tarea.value.split('\n').filter( x => x.trim().match( /[\s\w+]/ ) ).sort(),
o = {};
if(!val.length){alert('No words to count'); return;}
val.forEach( x => o[x] ? o[x]++ : o[x] = 1 );
tarea.value = Object.entries(o).map( a => `${a[0]} (${a[1]})` ).join('\n');
});
</script>
</body>
</html>
Вводим:
- Код: Выделить всё
cloud hosting
escape characters
escape characters
faster databases
for mission
for premium
hiring blogdocsget
hosting app
hosting cloud
lives upport
maintenance spaces
mission critical
need live
need live
need live
need live
need live
storage volumes
storage volumes
storage volumes
Получаем:
- Код: Выделить всё
cloud hosting (1)
escape characters (2)
faster databases (1)
for mission (1)
for premium (1)
hiring blogdocsget (1)
hosting app (1)
hosting cloud (1)
lives upport (1)
maintenance spaces (1)
mission critical (1)
need live (5)
storage volumes (3)