简体   繁体   English

Javascript:在不分割单词的情况下插入第n个字符,并将nbsp添加到计数中

[英]Javascript: insert   every nth character without splitting words and adding the nbsp into the count

Without going too much into detail, some basic facts: I'm not a web developer, I don't really know javascript, just enough to make me dangerous. 在不做过多详细介绍的情况下,请注意一些基本事实:我不是Web开发人员,我不太了解javascript,足以使我感到危险。 We utilize a website integration third-party software, that takes data from our ERP inventory system, and creates web pages, for products, ordering, etc... and then syncs it back into our ERP, using an SQL database. 我们利用网站集成第三方软件,该软件从我们的ERP库存系统中获取数据,并创建产品,订购等的网页,然后使用SQL数据库将其同步回我们的ERP。 Part of this packages includes an HTML editor that was added to our inventory system to help create listings. 该软件包的一部分包括一个HTML编辑器,该编辑器已添加到我们的库存系统中,以帮助创建清单。 This HTML code is then broken up into SQL varchar(250). 然后,此HTML代码被分解为SQL varchar(250)。 When the website is ultimately generated we end up with merged words every 250 characters. 当网站最终生成时,我们最终每250个字符合并一个单词。 To keep the examples shorter, and because the solution shouldn't matter, let's assume it's varchar(100) and merges words every 100 characters: 为了简化示例,并且由于解决方案无关紧要,我们假设它是varchar(100)并每100个字符合并一个单词:

Example: This is an example and it runs 140 characters long, and this is how you would expect it to look on a website when presented to the end user. 示例:这是一个示例,它的长度为140个字符,这是呈现给最终用户时您希望它在网站上显示的方式。

Our Website: This is an example and it runs 140 characters long, and this is how you would expect it to look on awebsite when presented to the end user. 我们的网站:这是一个示例,它运行140个字符,这是当您向最终用户展示它时,您希望它在网站上显示的样子。

I've merged the words "a" and "website" to be "awebsite" as that is what happens. 我将单词“ a”和“网站”合并为“ awebsite”,因为那样会发生。 It never breaks words, and always just removes the space before the word that would cause it to break 100 characters. 它从不打断单词,并且总是只删除单词之前的空格,以免它破坏100个字符。

Now, some are probably going to ask, "Why not have the third-party company fix this?" 现在,有些人可能会问:“为什么第三方公司没有解决此问题?” We tried, it was a costly integration, and they could not figure out why or how to fix it after 2 months, and eventually said it was costing too much to fix the bug that they were not going to solve it, and if we wanted to we could pay more to have them come up with a solution. 我们尝试过,这是一个昂贵的集成,并且他们无法弄清楚为什么或如何在2个月后修复它,最终说修复他们不打算解决的错误的成本太高了,如果我们想要我们可以花更多钱让他们提出解决方案。

We came up with our own solution, simply find the 100th character, and right before whatever word that was insert a so my HTML code looked like this: 我们想出了自己的解决方案,只需找到第100个字符,然后在要插入的单词之前,我的HTML代码如下所示:

<p>This is an example and it runs 140 characters long, and this is how you would expect it to look on a &nbsp;website when presented to the end user.</p>

Okay, this works, but its very labor intensive when you have 500+ character count descriptions, and the SQL table takes into consideration all code as characters, every time we manually add a &nbps; 好的,这是可行的,但是当您有500个以上的字符计数描述时,这非常费力,并且每次我们手动添加&nbps;时SQL表都将所有代码都视为字符。 it adds 6 characters, making us need to recount the next string etc... 它添加了6个字符,这使我们需要重新计算下一个字符串等。

So I'm trying to create a simple form, easy enough: 所以我试图创建一个简单的表单,足够简单:

 $(function () { $("#btnSubmitTwo").on("click", function () { var html = "<p>{{inputHTML}}</p>"; $(".input").each(function (i, v) { v = $(v); html = html.replace(new RegExp("{{" + v.attr("id") + "}}", 'g'), v.val()); html = html.replace(/ /g, '&nbsp;'); }); $("#htmlResultHTML").val(html); }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container-fluid"> <div class="row"> <div class="col-xs-6 col-sm-1">Description:</div> <div class="col-xs-6 col-sm-11"><textarea cols="82" rows="5" type="text" id="inputHTML" class="input"></textarea> <small id="fitmentinline" class="text-muted">Enter Description.</small> </div> </div> <div class="row"> <div class="col-xs-12"> <button class="btn btn-primary" id="btnSubmitTwo">Submit</button> </div> </div> <div class="row"> <div class="col-xs-12"> <textarea readonly id="htmlResultHTML" cols="100" rows="100"></textarea> </div> </div> </div> 

This was my attempt at a solution, just to replace all spaces with hard coded spaces. 这是我尝试的一种解决方案,只是用硬编码空间替换所有空间。 But of course this inserts a new problem. 但这当然会带来一个新问题。 Strings now break divs/tables etc... and just run on in a single line until they end, web browsers cannot word wrap this solution. 现在,字符串会破坏div /表等...,并且只能在一行中运行直到结束,Web浏览器无法对此解决方案进行自动换行。 So, I tried to find more advanced solutions, but I am not skillful enough to merge these solutions into one that solves my specific problem. 因此,我试图找到更高级的解决方案,但是我不够熟练,无法将这些解决方案合并为解决我的特定问题的解决方案。

Goal: To have that simple form, that spits out a HTML code, but instead of inserting a nbsp; 目标:拥有一个简单的格式,它会吐出HTML代码,而不是插入nbsp; in EVERY space, only have it insert a nbsp; 在每个空间中,只能插入一个nbsp; right after the space after the last full word before reaching 100 characters. 在最后一个完整单词之后的空格之后,到达100个字符之前。 However it cannot replace the space that is there, just add a new hard-coded one. 但是,它无法替换那里的空间,只能添加一个新的硬编码空间。 Then repeat itself and count the next 100 characters, including the +6 characters added by inserting the nbps; 然后重复自己并计算接下来的100个字符,包括通过插入nbps添加的+6个字符;

So we would end up with something that looked identical to our manual code: 因此,我们最终得到的内容与我们的手动代码相同:

<p>This is an example and it runs 140 characters long, and this is how you would expect it to look on a &nbsp;website when presented to the end user.</p>

And taken further: 并进一步:

<p>This is an example and it runs 140 characters long, and this is how you would expect it to look on a &nbsp;website when presented to the end user. This is an example and it runs 140 characters long, &nbsp;and this is how you would expect it to look on a website when presented to the end user.</p>

Here is the research I've done so far, but have unsuccessfully tried to implement: 这是我到目前为止所做的研究,但未成功实现:

How can I set a character limit of 100 without splitting words? 如何在不分割单词的情况下将字符限制设置为100?

How can I insert a character after every n characters in javascript? 如何在javascript中每n个字符后插入一个字符?

Here you go. 干得好。 Should be pretty good. 应该还不错。

var testString = "This is a long sentence for testing the code made. This is a long sentence for testing the code made.This is a long sentence for testing the code made.";
var output = "";
var arr = testString.split(" ");

var maxLength = 50;
var section = "";

for (var i = 0; i <= arr.length; i++) {
  var v = section + arr[i];
  if (v.length >= maxLength) {
    if(output.length != 0) output += "&nbsp;";
    output += section;
    section = arr[i] + " ";
  }
  else if(arr.length == i){
    output += section;
  }
  else{
    section = v + " ";
  }
}
console.info(output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM