首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用JS的数字之后,自动将空格转换为不间断的空格。

在使用JS的数字之后,自动将空格转换为不间断的空格。
EN

Stack Overflow用户
提问于 2017-03-24 18:24:30
回答 3查看 1.3K关注 0票数 0

标题说明了一切,如何能够使用Javascript将所有空格转换为不间断的空格,按照的html中的每一个数进行转换?

编辑我试过了(来源):

代码语言:javascript
复制
Prevent line-breaks after numbers
  this.preventBreaks = function(){
  	/* */
  	var id = $('.container');
  	/* edit pattern here */
  	/* */
  	var obj = document.getElementById(id);
  	if(obj){
  		var html = obj.innerHTML;
  		var nhtml = html.replace(pattern,function(r){return ""+r+""})
  		obj.innerHTML = nhtml;
  	};
  };

还有很多其他的事情搅乱了.replace(/(\d+)/g。但Javascript对我来说还是个谜。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-03-25 22:20:33

一个解决办法是:

代码语言:javascript
复制
var tree = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);

 while (tree.nextNode()) {
     var textNode = tree.currentNode;
     textNode.nodeValue = textNode.nodeValue.replace(/(\d)\s/gu, '$1\xa0');
 }
代码语言:javascript
复制
<h1>Lorem 4 ipsum, lorem 5 ipsum…</h1>
<p>That text will not let any number alone at the end of the lines. I've got 99 problems but non-breaking spaces after numbers aren't one anymore! I've got 99 problems but non-breaking spaces after numbers aren't one anymore! I've got 99 problems but non-breaking spaces after numbers aren't one anymore!</p>

(可能不适用于IE7)。

对于输入和正则表达式,所有的信用都会转到@zer00ne,@c.P.u1 (来源)!

票数 0
EN

Stack Overflow用户

发布于 2017-03-24 19:22:53

&nbsp;很棘手,最简单的方法就是从这样的站点复制和粘贴它。详细信息在代码段中有注释。

片段1

代码语言:javascript
复制
/* capture: ( any number: \d) that has a
|| whitespace: \s following it.
|| global: g, allows the search to continue 
|| after the first target is found.
|| unicode: u, will accept unicode in 
|| various ways, this snippet is using
|| copy and paste of &nbsp;
*/
var rgx = /(\d)\s/gu;
/* Using string literal by using the backtick: `
|| instead of quotes. This allows us to use
|| multi-line strings
*/
var str = `abcd 
efghi 123 456
789  10a11 b12c 01`;
/* .replace the regex matches with the original
|| content: $1 followed by a &nbsp;(or \u00a0)
|| Although it's invisible the space following
|| $1 is &nbsp;. evident in results
*/
var res = str.replace(rgx, '$1 ');
/* Note the string has less breaks only one 
|| remains since it is after a letter
*/
console.log(res);

片段2

代码语言:javascript
复制
// Get the innerHTML of parent element
var main = document.querySelector('main').innerHTML;


// Convert it into a string
var str = main.toString();


var rgx = /(\d)\s/gu;

var res = str.replace(rgx, '$1 ');

console.log(res);

var M = document.querySelector('main');

M.insertAdjacentHTML('afterbegin', res);
代码语言:javascript
复制
<main>
  <p>the number pi 3.14
  goes on forever, because its running from chuck norris. </p>

  <p>The leading causes of death in the United States are: 1 Heart Disease 2
  Chuck Norris 3
  Cancer. Chuck Norris drives an ice cream truck covered in human skulls, Chuck Norris is so fast, he can run around the world and punch himself in the back of the head. Crop circles are Chuck Norris' way of telling the world that sometimes corn needs to lie down Crop circles are Chuck Norris' way of telling the world that sometimes corn needs to lie down, If you spell Chuck Norris in Scrabble, you win. Forever, Chuck Norris told his iPhone 2g it was a iPhone 4
  He can now multi task and use face time, Chuck Norris counted to infinity - 2
    times, Chuck Norris is the only man to ever defeat a brick wall in a game of tennis. Chuck Norris invented Kentucky Fried Chicken's famous secret recipe, with 11
    herbs and spices. But nobody ever mentions the 1 
    of 12
    ingredients: Fear. Chuck Norris doesn't wear a watch. HE decides what time it is Chuck Norris uses pepper spray to spice up his steaks. </p>

  <p>Chuck Norris drives an ice cream truck covered in human skulls. Chuck Norris drives an ice cream truck covered in human skulls When the Boogeyman goes to sleep every night, he checks his closet for Chuck Norris. The chief export of Chuck Norris is Pain There is no theory of evolution. Just a list of animals Chuck Norris allows to live, Chuck Norris doesn't wear a watch. HE decides what time it is, Chuck Norris can slam a revolving door. </p>

  <p>Chuck Norris drives an ice cream truck covered in human skulls, Chuck Norris doesn't wear a watch. HE decides what time it is. Chuck Norris is 10
  feet tall, weighs two-tons, breathes fire, and could eat a hammer and take a shotgun blast standing. Chuck Norris doesn't churn butter. He roundhouse kicks the cows and the butter comes straight out, Fool me once, shame on you. Fool Chuck Norris 1
  time and he will roundhouse you in the face Chuck Norris is my Homeboy, Chuck Norris doesn't wear a watch. HE decides what time it is, Chuck Norris doesn't wash his clothes, he disembowels them, Chuck Norris 1
  time roundhouse kicked someone so hard that his foot broke the speed of light, went back in time, and killed Amelia Earhart while she was flying
    over the Pacific Ocean Fool me 1
    time, shame on you. Fool Chuck Norris once and he will roundhouse you in the face. Fool me 1
    time, shame on you. Fool Chuck Norris 1
    time and he will roundhouse you in the face. Chuck Norris is so fast, he can run
    around the world and punch himself in the back of the head. </p>
</main>

代码语言:javascript
复制
var main = document.querySelector('main');

main.normalize();

/* If we want the whole page to be normalized:
|| document.normalize();
*/
代码语言:javascript
复制
<main>
<p>the number pi 3.14
      goes on forever, because its running from chuck norris. </p>

      <p>The leading causes of death in the United States are: 1 Heart Disease 2
      Chuck Norris 3
      Cancer. Chuck Norris drives an ice cream truck covered in human skulls, Chuck Norris is so fast, he can run around the world and punch himself in the back of the head. Crop circles are Chuck Norris' way of telling the world that sometimes corn needs to lie down Crop circles are Chuck Norris' way of telling the world that sometimes corn needs to lie down, If you spell Chuck Norris in Scrabble, you win. Forever, Chuck Norris told his iPhone 2g it was a iPhone 4
      He can now multi task and use face time, Chuck Norris counted to infinity - 2
        times, Chuck Norris is the only man to ever defeat a brick wall in a game of tennis. Chuck Norris invented Kentucky Fried Chicken's famous secret recipe, with 11
        herbs and spices. But nobody ever mentions the 1 
        of 12
        ingredients: Fear. Chuck Norris doesn't wear a watch. HE decides what time it is Chuck Norris uses pepper spray to spice up his steaks. </p>

      <p>Chuck Norris drives an ice cream truck covered in human skulls. Chuck Norris drives an ice cream truck covered in human skulls When the Boogeyman goes to sleep every night, he checks his closet for Chuck Norris. The chief export of Chuck Norris is Pain There is no theory of evolution. Just a list of animals Chuck Norris allows to live, Chuck Norris doesn't wear a watch. HE decides what time it is, Chuck Norris can slam a revolving door. </p>

      <p>Chuck Norris drives an ice cream truck covered in human skulls, Chuck Norris doesn't wear a watch. HE decides what time it is. Chuck Norris is 10
      feet tall, weighs two-tons, breathes fire, and could eat a hammer and take a shotgun blast standing. Chuck Norris doesn't churn butter. He roundhouse kicks the cows and the butter comes straight out, Fool me once, shame on you. Fool Chuck Norris 1
      time and he will roundhouse you in the face Chuck Norris is my Homeboy, Chuck Norris doesn't wear a watch. HE decides what time it is, Chuck Norris doesn't wash his clothes, he disembowels them, Chuck Norris 1
      time roundhouse kicked someone so hard that his foot broke the speed of light, went back in time, and killed Amelia Earhart while she was flying
        over the Pacific Ocean Fool me 1
        time, shame on you. Fool Chuck Norris once and he will roundhouse you in the face. Fool me 1
        time, shame on you. Fool Chuck Norris 1
        time and he will roundhouse you in the face. Chuck Norris is so fast, he can run
        around the world and punch himself in the back of the head. </p>
    </main>

片段3

票数 1
EN

Stack Overflow用户

发布于 2017-03-24 18:31:57

应该用正则表达式替换如下:

代码语言:javascript
复制
var input = '10 USD';

var output = input.replace(/(\d) /g, '$1&nbsp;');

console.log(output); // "10&nbsp;USD"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43006663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档