How can I replace underscores with spaces using a regex in Javascript?
如何用Javascript中的regex替换下划线?
var ZZZ = "This_is_my_name";
4 个解决方案
#1
42
If it is a JavaScript code, write this, to have transformed string in ZZZ2
:
如果是JavaScript代码,写下来,在ZZZ2中转换字符串:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.replace(/_/g, " ");
also, you can do it in less efficient, but more funky, way, without using regex:
同样,你可以用更低的效率,但更时髦的方式,不使用regex:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.split("_").join(" ");
#2
1
Regular expressions are not a tool to replace texts inside strings but just something that can search for patterns inside strings. You need to provide a context of a programming language to have your solution.
正则表达式不是替代字符串中的文本的工具,而是可以搜索字符串中的模式的工具。您需要提供编程语言的上下文才能获得解决方案。
I can tell you that the regex _
will match the underscore but nothing more.
我可以告诉您,regex _将匹配下划线,但仅此而已。
For example in Groovy you would do something like:
例如,在Groovy中,您可以执行以下操作:
"This_is_my_name".replaceAll(/_/," ")
===> This is my name
but this is just language specific (replaceAll
method)..
但这只是特定于语言的方法(replaceAll方法)。
#3
0
Replace "_" with " "
将"_"替换为" "
The actual implementation depends on your language.
实际的实现取决于您的语言。
In Perl it would be:
在Perl中,它将是:
s/_/ /g
But the truth is, if you are replacing a fixed string with something else, you don't need a regular expression, you can use your language/library's basic string replacement algorithms.
但事实是,如果你用别的东西替换一个固定的字符串,你不需要一个正则表达式,你可以使用你的语言/库的基本字符串替换算法。
Another possible Perl solution would be:
另一个可能的Perl解决方案是:
tr/_/ /
#4
0
var str1="my__st_ri_ng";
var str2=str1.replace(/_/g, ' ');
#1
42
If it is a JavaScript code, write this, to have transformed string in ZZZ2
:
如果是JavaScript代码,写下来,在ZZZ2中转换字符串:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.replace(/_/g, " ");
also, you can do it in less efficient, but more funky, way, without using regex:
同样,你可以用更低的效率,但更时髦的方式,不使用regex:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.split("_").join(" ");
#2
1
Regular expressions are not a tool to replace texts inside strings but just something that can search for patterns inside strings. You need to provide a context of a programming language to have your solution.
正则表达式不是替代字符串中的文本的工具,而是可以搜索字符串中的模式的工具。您需要提供编程语言的上下文才能获得解决方案。
I can tell you that the regex _
will match the underscore but nothing more.
我可以告诉您,regex _将匹配下划线,但仅此而已。
For example in Groovy you would do something like:
例如,在Groovy中,您可以执行以下操作:
"This_is_my_name".replaceAll(/_/," ")
===> This is my name
but this is just language specific (replaceAll
method)..
但这只是特定于语言的方法(replaceAll方法)。
#3
0
Replace "_" with " "
将"_"替换为" "
The actual implementation depends on your language.
实际的实现取决于您的语言。
In Perl it would be:
在Perl中,它将是:
s/_/ /g
But the truth is, if you are replacing a fixed string with something else, you don't need a regular expression, you can use your language/library's basic string replacement algorithms.
但事实是,如果你用别的东西替换一个固定的字符串,你不需要一个正则表达式,你可以使用你的语言/库的基本字符串替换算法。
Another possible Perl solution would be:
另一个可能的Perl解决方案是:
tr/_/ /
#4
0
var str1="my__st_ri_ng";
var str2=str1.replace(/_/g, ' ');