Reputation: 12729
I am practicing javascript questions. I want to split
a string which have -.,
. how to pass all three in split function
I my first test case is passed if I used like this str.split('-')
but other test case failed ?
s
entensify("May-the-force-be-with-you") should return "May the force be with you".
sentensify("The.force.is.strong.with.this.one") should return "The force is strong with this one".
sentensify("There,has,been,an,awakening") should return "There has been an awakening".
any better solution to pass all test cases
function sentensify(str) {
//console.log(str.split(/\s*(?:-;|$)\s*/).join(' '))
return str.split('-').join(' ')
}
Upvotes: 0
Views: 135
Reputation: 1
/\W/
is a regular expression that matches any character that is not a word, includes spaces and punctuation, but does not include underscores.
function sentensify(str) {
return str.split(/\W/).join(" ")
}
console.log(sentensify("The.force.is.strong.with.this.one"));
Upvotes: 0
Reputation: 1327
Regex may be used for multiple pattern match; however, if you're looking break on the token and then join with a space, you may want to consider using the replace()
method.
For convenience, I've placed an example in a test suite:
function sentensify(str) {
return str.replace(/[-.,]/g, ' ')
}
mocha.setup('bdd')
describe('sentensify', function () {
let expectation = 'This is a test'
it('translates string from hyphens to spaces', function () {
expect(sentensify('This-is-a-test')).to.equal(expectation);
});
it('translates string from periods to spaces', function () {
expect(sentensify('This.is.a.test')).to.equal(expectation);
});
it('translates string from commas to spaces', function () {
expect(sentensify('This,is,a,test')).to.equal(expectation);
});
it('translates hyphens, commas, and strings to spaces', function () {
expect(sentensify('This-is,a.test')).to.equal(expectation);
});
});
mocha.run()
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/7.1.0/mocha.min.css" integrity="sha256-zX6KvfdhTfwaVsK5hogXY+wWW9Nf3wyloswn7WLZ9dg=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/7.1.0/mocha.min.js" integrity="sha256-lDr02Gr5QIpJQ/k0/Bxx80/1G80tLKt/lruAK8bIYX0=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.3.0/index.min.js" integrity="sha256-eIk9dgZF9fcJLuPyL6F9ZnhgmRPnG0OLM9Yyh5qLODU=" crossorigin="anonymous"></script>
<div id="mocha"></div>
Upvotes: 0
Reputation: 68933
You can use RegEx specifying all the characters on which you want to split the string, like /[-.,]/
.
Multiple characters inside square brackets [-.,]
means searching for any character among the given characters.
function sentensify(str) {
return str.split(/[-.,]/).join(' ');
}
console.log(sentensify("May-the-force-be-with-you"));
console.log(sentensify("The.force.is.strong.with.this.one"));
console.log(sentensify("There,has,been,an,awakening"));
Upvotes: 1
Reputation: 2501
This works to match any of . , -
. The special regex characters should be escaped with backslashes.
function sentensify(str) {
return str.split(/[\.,\-]/).join(" ")
}
Upvotes: 0
Reputation: 3434
You can use a regex for this to match all three. But if you're just going to join
the array, it might make more sense to just replace()
them with a space.
split()
var str= "Instead-of-using-split,and,join.why.not-just-use-replace?"
var sep = /[-,.]/g;
console.log( str.split(sep).join(' ') );
replace()
var str= "Instead-of-using-split,and,join.why.not-just-use-replace?"
var sep = /[-,.]/g;
console.log( str.replace(sep, ' ') );
Upvotes: 0