Here is an explanation for each of the terms:
$(this).val().replace(/^\s+|\s+$/g, '');
This replaces any leading spaces or trailing spaces with blanks:
" Hello" -> "Hello"
"Hello " -> "Hello"
" Hello " -> "Hello"
a = a.replace(/^-+|-+$/g, '');
This replaces any leading -
(1 or more times) or trailing -
(1 or more times) with blanks.
"Hello----" -> "Hello"
"--- Hello ---" -> " Hello "
"-H-E-L-L-O-" -> "H-E-L-L-O"
a = a.replace(/[^\w-]/g, '-');
This replaces any character that is not (^) a word character (in JavaScript, it is defined to include lowercase and uppercase English alphabets, digits 0 to 9 and underscores), or not a '-' with '-'.
" Hello" -> "----Hello"
"Hello@@@@" -> "Hello----"
"Hello--123___" -> "Hello--123___"
"@£"$%"!$" -> ""
a = a.replace(/--+/g, '-');
This replaces 2 or more consecutive -
with a single '-'.
"--" -> "-"
"Hello--" -> "Hello-"
"------Hello---------" -> "-Hello-"
"H--ello---------" -> "H-ello-"