Zastąp wszystkie białe znaki

Chcę zastąpić wszystkie wystąpienia białych znaków spacji (spacja, tabulator, nowa linia) w JavaScript.
Jak to zrobić?

Próbowałem:

str.replace(/ /gi, "X")
Author: Stefan van den Akker, 2011-06-28

8 answers

Chcesz \s

Dopasowuje pojedynczą białą spację znak, w tym spacja, tabulator, forma / align = "left" /

Odpowiednik

[ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]

W Firefox i [ \f\n\r\t\v] w IE .


str = str.replace(/\s/g, "X");
 231
Author: Alex K.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-06 10:04:50

\s jest meta znak, który obejmuje wszystkie białe spacje. Nie musisz rozróżniać wielkości liter - białe spacje nie mają wielkości liter.

str.replace(/\s/g, "X")
 24
Author: Quentin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-28 13:19:09

Możemy również użyć tego, jeśli chcemy zmienić wszystkie spacje połączone pojedynczym znakiem:

str.replace(/\s+/g,'X');
 8
Author: Milos Stankovic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-20 09:51:37

Czy próbowałeś \s?

str.replace(/\s/g, "X");
 7
Author: Michael Berkowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-28 13:20:13

Spróbuj tego:

str.replace(/\s/gi, "X")
 3
Author: Headshota,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-28 13:19:38

Faktycznie to działa, ale

Spróbuj tego.

Weź wartość / \s / g do zmiennej łańcuchowej, takiej jak

String a = /\s/g;

str = str.replaceAll(a,"X");
 1
Author: Siten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-28 13:43:25

Nie / gi ale / g

var fname = "My Family File.jpg"
fname = fname.replace(/ /g,"_");
console.log(fname);

Daje

"My_Family_File.jpg"
 1
Author: jimver04,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-23 13:26:39

Użyłem metody "slugify" z underscore.Sznurek i zadziałał jak czar:

Https://github.com/epeli/underscore.string#slugifystring--string

Najfajniejsze jest to, że można po prostu zaimportować tę metodę, nie trzeba importować całej biblioteki.

 0
Author: dude,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-06 12:28:57