简体   繁体   English

替换字符串中的特定字符

[英]replacing specific characters in strings

pretty simple: 很简单:

i have mathematical problems stored in a DB, like 3+6 , 5*3 , 4-2 etc. 我将数学问题存储在数据库中,例如3+6 4-2 5*3 4-2等。

i want the output to show proper mathematical ×s instead of * (and ÷ instead of /). 我希望输出显示正确的数学×而不是*(和÷而不是/)。 but they have to be stored in the DB with * and / for obvious reasons (being "normal" characters vs. html entities in DB and especially for mathjs to be able to solve them from string) 但是出于明显的原因,它们必须使用*和/存储在数据库中(在数据库中是“普通”字符与html实体,尤其是mathjs能够从字符串中解决它们)

so i am looking for a way to change them in the html output. 所以我正在寻找一种在html输出中更改它们的方法。 first i thought about something with css, but that would probably mean i'd have to have a class for them (or is it possible?). 首先,我考虑过使用CSS的问题,但这可能意味着我必须为他们准备一门课(或者可能吗?)。 then i thought i could do it with jS/jQuery. 那我以为我可以用jS / jQuery做到。 but it feels overly complicated at first. 但起初感觉过于复杂。

how would you do this? 你会怎么做?

(server is running node.js + jade. the strings come from the db and are rendered directly on the page, so i need a way to change the symbols "afterwards") (服务器正在运行node.js + jade。字符串来自db,直接在页面上呈现,因此我需要一种方法来更改“之后”符号)

You don't necessarily have to store the characters as HTML entities. 您不必将字符存储为HTML实体。 So long as you include the appropriate charset meta tag, then you'll be able to use unicode symbols in your page. 只要您包含适当的charset元标记,就可以在页面中使用unicode符号。

<meta charset='utf-8'>

If you can't store them in the database as unicode, then you'll have to programatically fix the strings afterwards. 如果您不能将它们以unicode的形式存储在数据库中,则随后必须以编程方式修复字符串。

var replacementSymbols: {
  '*': '×',
  '/': '÷'
};

function replaceInString(equation) {
  var targetSymbols = Object.keys(replacementSymbols);

  return targetSymbols.reduce(function(string, symbol) {
    var replacement = replacementSymbols[symbol];
    return string.replace(symbol, replacement);
  }, equation);
}

What you're attempting to do is not really possible with CSS. CSS确实无法实现您想做的事情。

You could use JavaScript's string replace method to achieve this instead. 您可以使用JavaScript的字符串替换方法来实现此目的。 for instance: 例如:

var str = "5*6"
var res = str.replace("*", "×");

http://www.w3schools.com/jsref/jsref_replace.asp http://www.w3schools.com/jsref/jsref_replace.asp

When you're writing your html, you can insert a <span class='multiply'></span> for each * 在编写html时,您可以为每个*插入<span class='multiply'></span>

Then you can do: 然后,您可以执行以下操作:

$("span.multiply").html("×");

And then you you would do the same with division, and so on. 然后您将对除法进行相同的操作,依此类推。

EDIT 编辑

You could also try something like this: 您也可以尝试如下操作:

var newHtml = $('body').html().replace(/\*/g, "×");
$('body').html(newHtml);

Unless you're using something like Angular and a filter to parse the string, something like this might be the best option. 除非您使用Angular之类的东西和一个过滤器来解析字符串,否则类似的东西可能是最好的选择。 If you're not able to insert classes before the page is rendered, you may need to see if the rendered data is wrapped in something like <pre/> tag so that you can use the appropriate selector: 如果无法在呈现页面之前插入类,则可能需要查看呈现的数据是否用<pre/>标记包裹,以便可以使用适当的选择器:

template 模板

<div class="math-expression">
    3 * 4 = 12
</div>

logic 逻辑

document.querySelectorAll('.math-expression').forEach( 
    function( elem, i, arr){
         s = elem.textContent
         elem.textContent = s.replace( /\*/g, 'x' )
    }
 )

note 注意

I didn't test this as I normally just use Angular filters for these types of text renderings. 我没有对此进行测试,因为我通常只对这些类型的文本呈现使用Angular过滤器。

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

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