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")
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");
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")
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');
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");
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")
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");
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"
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.
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