admin管理员组文章数量:1025482
I have the following code:
var string = "word1;word2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('\\w+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
// ends_with(string, ';') => ["word1", "word2"]
The function takes no regard to whitespace. For example if you input
ends_with('Jonas Sand,', ',')
the output will be Sand. Need help on making the function work with words that has whitespace.
I have the following code:
var string = "word1;word2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('\\w+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
// ends_with(string, ';') => ["word1", "word2"]
The function takes no regard to whitespace. For example if you input
ends_with('Jonas Sand,', ',')
the output will be Sand. Need help on making the function work with words that has whitespace.
Share Improve this question edited Dec 10, 2009 at 14:20 Dominic Rodger 99.9k37 gold badges203 silver badges217 bronze badges asked Dec 10, 2009 at 14:14 user228720user228720 1034 silver badges9 bronze badges 1- 1 Please put your code in code blocks - CTRL+K :) – Justin Swartsel Commented Dec 10, 2009 at 14:16
4 Answers
Reset to default 4You can use your separator within split
and take all except the last part with slice
:
function ends_with(string, character) {
return string.split(character).slice(0, -1);
}
\w
matches word characters, use [^x]
instead, where x
is your character. This matches everything but your character.
So the first line in your function bees
var regexp = new RegExp('[^' + character + "]+" + character, 'g');
on the other hand, if you want to match words separated by white space, use
var regexp = new RegExp('(\\w|\\s)+" + character, 'g');
PS: but isn't there a String#split
function in javascript?
Try using '[\\w\\s]+'
instead of '\\w+'
to include whitespace.
Try the following:
var string = "word1;w ord2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('.+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
I have the following code:
var string = "word1;word2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('\\w+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
// ends_with(string, ';') => ["word1", "word2"]
The function takes no regard to whitespace. For example if you input
ends_with('Jonas Sand,', ',')
the output will be Sand. Need help on making the function work with words that has whitespace.
I have the following code:
var string = "word1;word2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('\\w+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
// ends_with(string, ';') => ["word1", "word2"]
The function takes no regard to whitespace. For example if you input
ends_with('Jonas Sand,', ',')
the output will be Sand. Need help on making the function work with words that has whitespace.
Share Improve this question edited Dec 10, 2009 at 14:20 Dominic Rodger 99.9k37 gold badges203 silver badges217 bronze badges asked Dec 10, 2009 at 14:14 user228720user228720 1034 silver badges9 bronze badges 1- 1 Please put your code in code blocks - CTRL+K :) – Justin Swartsel Commented Dec 10, 2009 at 14:16
4 Answers
Reset to default 4You can use your separator within split
and take all except the last part with slice
:
function ends_with(string, character) {
return string.split(character).slice(0, -1);
}
\w
matches word characters, use [^x]
instead, where x
is your character. This matches everything but your character.
So the first line in your function bees
var regexp = new RegExp('[^' + character + "]+" + character, 'g');
on the other hand, if you want to match words separated by white space, use
var regexp = new RegExp('(\\w|\\s)+" + character, 'g');
PS: but isn't there a String#split
function in javascript?
Try using '[\\w\\s]+'
instead of '\\w+'
to include whitespace.
Try the following:
var string = "word1;w ord2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('.+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
本文标签: javascriptSplit String at specific characterStack Overflow
版权声明:本文标题:javascript - Split String at specific character - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745629202a2160056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论