简体   繁体   English

使用正则表达式在jQuery中将字符前置为字符串

[英]Prepend Character to String in jQuery with Regular Expression

I'm trying to add a character to the beginning of certain strings with Regular Expressions but am very new to it and can't seem to find an answer for what I'm looking for. 我正在尝试使用正则表达式在某些字符串的开头添加一个字符,但我对它很新,似乎无法找到我正在寻找的答案。 I have strings like Below 1499 and Above 1900 and I want to add a $ to the beginning of the number strings. 我有像Below 1499Above 1900字符串,我想在数字字符串的开头添加$ Here's what I've got to locate the code (btw, these are all text characters in a div with a class of refinement_price_text): 这是我必须找到的代码(顺便说一下,这些都是div中的文本字符,带有一个refinement_price_text类):

$('.refinement_price_text').each(function(){
    console.log($(this).text().match(/\d{1,5}/g));
});

It logs them out to the console fine. 它将它们很好地记录到控制台。 They are logged as arrays with one item. 它们被记录为具有一个项目的数组。 I don't know how to prepend a dollar sign to them now though. 我现在不知道如何为他们添加一个美元符号。 I've tried prepend() and that doesn't work. 我试过prepend(),但这不起作用。 I've tried to set the match() as a variable but that didn't work. 我试图将match()设置为变量,但这不起作用。 I wanted to originally use replace() but I need to maintain the current values there and just add the dollar sign character to the beginning and I didn't know what the equivalent of $(this) is for a regular expression in order to keep the same values. 我想最初使用replace(),但我需要保持当前的值,只需将美元符号字符添加到开头,我不知道$(this)等价于正则表达式是为了保持相同的价值观。

Let me know if this is making sense. 如果这是有道理的,请告诉我。 I'm sure there must be a function that will easily do this? 我确定必须有一个能够轻松完成此功能的功能吗? Thanks for your help! 谢谢你的帮助!

I believe this handles all possibilities: 我相信这可以处理所有可能性:

"111 Above 1499 and below 14930 and $100".replace(/([^$]|^)(\b\d+)/g, "$1$$$2")
> "$111 Above $1499 and below $14930 and $100"

To replace the text in Jquery: 要替换Jquery中的文本:

$(this).text(function(i, t) { return t.replace(...above stuff...) })

http://jsfiddle.net/k7XJw/1/ http://jsfiddle.net/k7XJw/1/

To ignore numbers is parentheses, 忽略数字是圆括号,

str = "111 Above 1499 and below 14930(55) and $100 and (1234) and (here 123) and (123 there)"
str.replace(/([^$(]|^)(\b\d+\b)(?!\))/g, "$1$$$2")
> "$111 Above $1499 and below $14930(55) and $100 and (1234) and (here 123) and (123 there)"

For simple strings like the one you posted a lookahead regex and replace will work. 对于简单的字符串,比如你发布的前瞻性正则表达式和替换将工作。 Basically telling the regex to find the first number in a string (but dont consume it) and then prepend a dollar sign. 基本上告诉正则表达式找到字符串中的第一个数字(但不要消耗它)然后添加一个美元符号。 For multiple numbers in the same string you will have to tweak the regex. 对于同一个字符串中的多个数字,您必须调整正则表达式。

var s = "before 1900"
s=s.replace(/(?=[0-9])/,"$");
console.log(s);

Modified to support multiple occurences. 修改以支持多次出现。 It looks for any number preceeded by a whitespace and then prepends the dollar sign to that number. 它查找以空格开头的任何数字,然后将美元符号添加到该数字。

Plunker example Plunker的例子

var s = "before 1900 and 2130 and (1900)"
s=s.replace(/\s(?=\d)/g," $");
console.log(s);

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

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