Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript text word wrap replace

str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It w as popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

str = wordWrap(str, 40);

function wordWrap(str, maxWidth) {
    var newLineStr = "
"; done = false; res = '';
    while (str.length > maxWidth) {                 
        found = false;
        // Inserts new line at first whitespace of the line
        for (i = maxWidth - 1; i >= 0; i--) {
            if (testWhite(str.charAt(i))) {
                res = res + [str.slice(0, i), newLineStr].join('');
                str = str.slice(i + 1);
                found = true;
                break;
            }
        }
        // Inserts new line at maxWidth position, the word is too long to wrap
        if (!found) {
            res += [str.slice(0, maxWidth), newLineStr].join('');
            str = str.slice(maxWidth);
        }

    }

    return res + str;
}

function testWhite(x) {
    var white = new RegExp(/^s$/);
    return white.test(x.charAt(0));
};
Comment

javascript text word wrap replace

// Static Width (Plain Regex)
const wrap = (s) => s.replace(
    /(?![^
]{1,32}$)([^
]{1,32})s/g, '$1
'
);

// Dynamic Width (Build Regex)
const wrap = (s, w) => s.replace(
    new RegExp(`(?![^
]{1,${w}}$)([^
]{1,${w}})s`, 'g'), '$1
'
);
Comment

javascript text word wrap replace

function breakTextNicely(text, limit, breakpoints) {

      var parts = text.split(' ');
      var lines = [];
      text = parts[0];
      parts.shift();

      while (parts.length > 0) {
        var newText = `${text} ${parts[0]}`;

        if (newText.length > limit) {
          lines.push(`${text}
`);
          breakpoints--;

          if (breakpoints === 0) {
            lines.push(parts.join(' '));
            break;
          } else {
          	text = parts[0];
    	  }
        } else {
          text = newText;
        }
    	  parts.shift();
      }

      if (lines.length === 0) {
        return text;
      } else {
        return lines.join('');
      }
    }

    var mytext = 'this is my long text that you can break into multiple line sizes';
    console.log( breakTextNicely(mytext, 20, 3) );
Comment

javascript text word wrap replace

var protest = "France is actually the worlds most bad country consisting of people and president full of mentaly gone persons and the people there are causing the disturbance and very much problem in the whole of the world.France be aware that one day there will be no france but you will be highly abused of your bad acts.France go to hell.";

protest = protest.replace(/(.{100})/g, "$1<br>");

document.write(protest);
Comment

PREVIOUS NEXT
Code Example
Javascript :: call laravel route js 
Javascript :: find whitespace in string js 
Javascript :: bootstrap 5.1 3 tooltip not working 
Javascript :: ReferenceError: primordials is not defined 
Javascript :: js input type range on hover 
Javascript :: fetching iframe content JS 
Javascript :: owl-carousel only for mobile 
Javascript :: ref css in jsp 
Javascript :: How to remove title in material-table 
Javascript :: useeffect with cleanup 
Javascript :: vue js countdown timer 
Javascript :: how to send a message discord.js 
Javascript :: how to add two attay into object in javascript 
Javascript :: javascript string contains function 
Javascript :: how to change style of an element using javascript 
Javascript :: jquery each tr except first 
Javascript :: comments in json 
Javascript :: toggle class javascript 
Javascript :: getelementsbyclassname remove class 
Javascript :: angular {{}} new line 
Javascript :: Deep copy objects js JSON methods 
Javascript :: typescript how to mode json files when compile 
Javascript :: install node on ubuntu and debian 
Javascript :: export html table data to excel using javascript 
Javascript :: jquery select2 how to make dont close after select 
Javascript :: TypeError: sequelize.import is not a function 
Javascript :: get object value in node js 
Javascript :: get last string after / in javascript 
Javascript :: jquery toggleclass 
Javascript :: how to uncheck a radio button 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =