[英]Regex to get the text between two characters?
I want to replace a text after a forward slash and before a end parantheses excluding the characters.我想在正斜杠之后和结束括号之前替换不包括字符的文本。
My text:
<h3>notThisText/IWantToReplaceThis)<h3>
$('h3').text($('h3').text().replace(regEx, 'textReplaced'));
Wanted result after replace:
notThisText/textReplaced)
I have tried我试过了
regex = /([^\/]+$)+/ //replaces the parantheses as well
regex = \/([^\)]+) //replaces the slash as well
but as you can see in my comments neither of these excludes both the slash and the end parantheses.但正如您在我的评论中所看到的,这些都不排除斜杠和结尾的括号。 Can someone help?
有人可以帮忙吗?
A pattern like /(?<=\\/)[^)]+(?=\\))/
won't work in JS as its regex engine does not support a lookbehind construct.像
/(?<=\\/)[^)]+(?=\\))/
这样的模式在 JS 中不起作用,因为它的正则表达式引擎不支持后视构造。 So, you should use one of the following solutions:因此,您应该使用以下解决方案之一:
s.replace(/(\/)[^)]+(\))/, '$1textReplaced$2')
s.replace(/(\/)[^)]+(?=\))/, '$1textReplaced')
s.replace(/(\/)[^)]+/, '$1textReplaced')
s.replace(/\/[^)]+\)/, '/textReplaced)')
The (...)
forms a capturing group that can be referenced to with $
+ number, a backreference, from the replacement pattern. (...)
形成一个捕获组,可以从替换模式中使用$
+ number(反向引用)引用该组。 The first solution is consuming /
and )
, and puts them into capturing groups.第一个解决方案是使用
/
和)
,并将它们放入捕获组中。 If you need to match consecutive, overlapping matches, use the second solution ( s.replace(/(\\/)[^)]+(?=\\))/, '$1textReplaced')
).如果您需要匹配连续、重叠的匹配项,请使用第二种解决方案(
s.replace(/(\\/)[^)]+(?=\\))/, '$1textReplaced')
)。 If the )
is not required at the end, the third solution ( replace(/(\\/)[^)]+/, '$1textReplaced')
) will do.如果最后不需要
)
,则第三个解决方案( replace(/(\\/)[^)]+/, '$1textReplaced')
)就可以了。 The last solution ( s.replace(/\\/[^)]+\\)/, '/textReplaced)')
) will work if the /
and )
are static values known beforehand.如果
/
和)
是事先已知的静态值,则最后一个解决方案( s.replace(/\\/[^)]+\\)/, '/textReplaced)')
)将起作用。
var text = "notThisText/IWantToReplaceThis";
text = text.replace(/\/.*/, "/whatever");
output : "notThisText/whatever"`
You can use str.split('/')
您可以使用
str.split('/')
var text = 'notThisText/IWantToReplaceThis';
var splited = text.split('/');
splited[1] = 'yourDesireText';
var output = splited.join('/');
console.log(output);
Try Following: In your case startChar='/', endChar = ')', origString=$('h3').text()尝试以下:在你的情况下 startChar='/', endChar = ')', origString=$('h3').text()
function customReplace(startChar, endChar, origString, replaceWith){
var strArray = origString.split(startChar);
return strArray[0] + startChar + replaceWith + endChar;
}
First of all, you didn't define clearly what is the format of the text which you want to replace and the non-replacement part.首先,您没有明确定义要替换的文本和非替换部分的格式是什么。 For example,
例如,
notThisText
contain any slash /
? notThisText
包含任何斜杠/
吗?IWantToReplaceThis
contain any parentheses )
? IWantToReplaceThis
是否包含任何括号)
? Since there are too many uncertainties, the answer here only shows up the pattern exactly matches your example:由于不确定性太多,这里的答案仅显示与您的示例完全匹配的模式:
yourText.replace(/(\/).*?(\))/g, '$1textReplaced$2')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.