Вы можете добиться этого, используя приведенное ниже регулярное выражение, которое находит все не-ascii символы (также исключает непечатаемые символы ascii и исключает также расширенные ascii) и удаляет его с пустой строкой.
[^ -~]+
Этопри условии, что вы хотите сохранить только все печатаемые символы ASCII, которые варьируются от пробела (значение 32 для ascii) до тильды ~
, следовательно, использование этого набора символов [^ !-~]
и затем заменяет все одно или несколько пробелов наодин пробел
var str = `Determine the values of P∞ and E∞ for each of the following signals: b.
d.
f.
Periodic and aperiodic signals Determine whether or not each of the following signals is periodic:
b.
Determine whether or not each of the following signals is periodic. If a signal is periodic, specify its fundamental period.
b.
d.
Transformation of Independent variables A continuous-time signal x(t) is shown in Figure 1. Sketch and label carefully each of the following signals:
b. c.
d. e. f. Figure 1: Problem Set 1.4
Even and Odd Signals
For each signal given below, determine all the values of the independent variable at which the even part of the signal is guaranteed to be zero.
b.
d. -------------------------`;
console.log(str.replace(/[^ -~]+/g,'').replace(/\s+/g, ' '));
<!-- begin snippet: js hide: false console: true babel: false -->
console.log(str.replace(/[^ !-~]+/g,'').replace(/\s+/g, ' '));
Кроме того, если вы просто хотите разрешить все буквенно-цифровые символы и упомянутые специальные символы, то вы можете использовать это регулярное выражение, чтобы сначала сохранить все необходимые символы, используя это регулярное выражение,
[^ a-zA-Z0-9,.:;[\]()/\!@#$%^&*+_{}<>=?~|"-]+
Замените это пустой строкой, а затем замените один или несколько пробелов только одним пробелом.
var str = `Determine the values of P∞ and E∞ for each of the following signals: b.
d.
f.
Periodic and aperiodic signals Determine whether or not each of the following signals is periodic:
b.
Determine whether or not each of the following signals is periodic. If a signal is periodic, specify its fundamental period.
b.
d.
Transformation of Independent variables A continuous-time signal x(t) is shown in Figure 1. Sketch and label carefully each of the following signals:
b. c.
d. e. f. Figure 1: Problem Set 1.4
Even and Odd Signals
For each signal given below, determine all the values of the independent variable at which the even part of the signal is guaranteed to be zero.
b.
d. -------------------------`;
console.log(str.replace(/[^ a-zA-Z0-9,.:;[\]()/\!@#$%^&*+_{}<>=?~|"-]+/g,'').replace(/\s+/g, ' '));