10

I'm really struggling how to split the text at the closest string to the 47th character. How is this done?

var fulltext = document.getElementById("text").value;

var a = fulltext.slice(0, 47);
console.log(a);

var b = fulltext.slice(47, 47*2);
console.log(b);

var c = fulltext.slice(94, 47*3);
console.log(c);

Here is a JS Fiddle - http://jsfiddle.net/f5n326gy/5/

Thanks.

3
  • 3
    Closest next of previous, or not important? Commented Jan 3, 2015 at 19:08
  • 1
    why not simplifying questions as much as possible, and posting var fulltext = 'my text here directly and keep us focus directly on your problem' Commented Jan 3, 2015 at 19:40
  • Hi, as long as the three lines of text read okay, I don't mind which. @dfsq Commented Jan 3, 2015 at 19:50

2 Answers 2

11

If you are interested in just the first part, then use

var a = fulltext.match(/^.{47}\w*/)

See demo (fiddle) here.


If you want to split the entire string to multiple substrings, then use

var a = fulltext.match(/.{47}\w*|.*/g);

See demo (fiddle) here.

...and if you wish substrings do not start with the word separator (such as space or comma) and prefer to include it with the previous match, then use

var a = fulltext.match(/.{47}\w*\W*|.*/g);

See demo (fiddle) here.

Sign up to request clarification or add additional context in comments.

10 Comments

This seems to work nicely for the first line. Would you be able to show me on jsfiddle how to split into three lines? Thanks for your time.
@TristanKirkpatrick - I have updated my answer. If that is not what you were looking for, please clarify your needs.
@Ωmega That splits the text perfectly in the console log. How can I then use each line as a variable. (it is to pass to a jQuery plugin) e.g. 0 - 47 = a, 47 - 94 = b
@TristanKirkpatrick - [1,2].forEach(function(i){if(typeof a[i]==='undefined'){a[i]=''}})
@TristanKirkpatrick - Right after var a = fulltext.match... line, before you pass a[0], a[1] and a[2] to your another code.
|
8

You can find the next word boundary using indexOf method with fromIndex second parameter. After that you can use slice to get either left part or right one.

var fulltext = "The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.";    
var before = fulltext.slice(0, fulltext.indexOf(' ', 47));
var after  = fulltext.slice(fulltext.indexOf(' ', 47));
alert(before);
alert(after);

3 Comments

This does seem to work although with some texts it will select a previous word and display it twice. Is there a way around this?
Can you give me an example of such text? can't imagine when it can happen.
I have been pasting in text from the internet and can't remember the exact text, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.