如何使用JavaScript在HTML页面中查找空格?
我想使用JavaScript查找HTML页面中每个单词之间的空格 例如: 这是示例文本 在本例中,“this”和“is”以及后面的每个单词之间都有一个空格如何使用JavaScript在HTML页面中查找空格?,javascript,html,Javascript,Html,我想使用JavaScript查找HTML页面中每个单词之间的空格 例如: 这是示例文本 在本例中,“this”和“is”以及后面的每个单词之间都有一个空格 如何实现这一点?您可以使用正则表达式实现这一点: document.body.innerHTML.match(/\s/g); 您可以使用正则表达式来实现这一点: document.body.innerHTML.match(/\s/g); 我知道这不是一个正确的答案,但请尝试一下 var str = "this is sample text
如何实现这一点?您可以使用正则表达式实现这一点:
document.body.innerHTML.match(/\s/g);
您可以使用正则表达式来实现这一点:
document.body.innerHTML.match(/\s/g);
我知道这不是一个正确的答案,但请尝试一下
var str = "this is sample text";
首先修剪它
str = str.trim();
按空格(“”)拆分文本
空格应该是array.length-1我知道这不是一个正确的答案,但请尝试一下
var str = "this is sample text";
首先修剪它
str = str.trim();
按空格(“”)拆分文本
空白将是array.length-1这很简单,不知道您需要做什么,下面是一些代码,它将去掉所有HTML标记并给您空格和单词,然后您可以将其拆分,以便在一个数组中获得所有空格和单词,然后您可以遍历该数组并执行您喜欢的操作:
var body = document.getElementsByTagName("body")[0];
var words = body.replace(/(<([^>]+)>)/ig,"").split(" ");
var body=document.getElementsByTagName(“body”)[0];
var words=body.replace(/(]+)>)/ig,“”。split(“”);
这很简单,不知道你需要做什么,下面是一些代码,它将去掉所有HTML标记并给你空格和单词,然后你可以将它们拆分,这样你就可以在一个数组中获得所有空格和单词,然后你可以遍历该数组并做你想做的事情:
var body = document.getElementsByTagName("body")[0];
var words = body.replace(/(<([^>]+)>)/ig,"").split(" ");
var body=document.getElementsByTagName(“body”)[0];
var words=body.replace(/(]+)>)/ig,“”。split(“”);
好的。现在我已经阅读了这里的注释,这是整个代码,它将突出显示页面上的所有文本
recurseDomChildren(document.documentElement, true);
function recurseDomChildren(start, output)
{
var nodes;
if(start.childNodes)
{
nodes = start.childNodes;
loopNodeChildren(nodes, output);
}
}
function loopNodeChildren(nodes, output)
{
var node;
for(var i=0;i<nodes.length;i++)
{
node = nodes[i];
if(output)
{
outputNode(node);
}
if(node.childNodes)
{
recurseDomChildren(node, output);
}
}
}
function outputNode(node)
{
var whitespace = /^\s+$/g;
if(node.nodeType === 1)
{
console.log("element: " + node.tagName);
}else if(node.nodeType === 3)
{
//clear whitespace text nodes
node.data = node.data.replace(whitespace, "");
if(node.data)
{
debugger;
node.parentElement.style.background="yellow";
console.log("text: " + node.data);
}
}
}
recursedmchildren(document.documentElement,true);
函数recurseDomChildren(启动、输出)
{
var节点;
if(start.childNodes)
{
nodes=start.childNodes;
loopNodeChildren(节点,输出);
}
}
函数loopNodeChildren(节点,输出)
{
var节点;
对于(var i=0;iOK)。现在我已经阅读了这里的注释,这是整个代码,它将突出显示页面上的所有文本
recurseDomChildren(document.documentElement, true);
function recurseDomChildren(start, output)
{
var nodes;
if(start.childNodes)
{
nodes = start.childNodes;
loopNodeChildren(nodes, output);
}
}
function loopNodeChildren(nodes, output)
{
var node;
for(var i=0;i<nodes.length;i++)
{
node = nodes[i];
if(output)
{
outputNode(node);
}
if(node.childNodes)
{
recurseDomChildren(node, output);
}
}
}
function outputNode(node)
{
var whitespace = /^\s+$/g;
if(node.nodeType === 1)
{
console.log("element: " + node.tagName);
}else if(node.nodeType === 3)
{
//clear whitespace text nodes
node.data = node.data.replace(whitespace, "");
if(node.data)
{
debugger;
node.parentElement.style.background="yellow";
console.log("text: " + node.data);
}
}
}
recursedmchildren(document.documentElement,true);
函数recurseDomChildren(启动、输出)
{
var节点;
if(start.childNodes)
{
nodes=start.childNodes;
loopNodeChildren(节点,输出);
}
}
函数loopNodeChildren(节点,输出)
{
var节点;
对于(var i=0;i一旦找到它,您需要做什么?我需要使用css高亮显示。您将无法使用css高亮显示,因为没有可应用它的选择器。好的……那么我如何实现此功能?您必须使用下面的技术找到空格,然后用元素包装每个空格,例如span
。您认为如何一旦你找到它,你需要做什么?我需要使用css高亮显示。你将无法使用css高亮显示,因为没有可以应用它的选择器。好的……那么我如何实现这个功能呢?你必须使用下面的技术找到空格,然后用元素包装每个空格,例如span
。