What I want to do is take a string such as this.those.that
and get a substring to or from the nth occurrence of a character. So, from the start of the string to the 2nd occurrence of .
would return this.those
. Likewise, from the 2nd occurrence of .
to the end of the string would return that
. Sorry if my question is foggy, it's not that easy to explain. Also, please do not suggest making extra variables, and the result will be in a string and not an array.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- how to split a list into a given number of sub-lis
If you really want to stick to string methods, then:
You could do it without arrays, but it would take more code and be less readable.
Generally, you only want to use as much code to get the job done, and this also increases readability. If you find this task is becoming a performance issue (benchmark it), then you can decide to start refactoring for performance.
jsFiddle.
I'm perplexed as to why you want to do things purely with string functions, but I guess you could do something like the following:
However, I'd strongly advocate for something like alex's much simpler code.
Try this :
"qwe.fs.xczv.xcv.xcv.x".replace(/([^\.]*\.){**nth**}/, '');
- where is nth is the amount of occurrence to remove.Just in case somebody needs both "this" and "those.that" in a way as alex described in his comment, here is a modified code: