JavaScript: Replacing characters on both sides of a string - javascript

What I want to do is to match characters enclosed by ^^ and replace those ^^ while maintaining the string. In other words, turning this:
^^This is a test^^ this is ^^another test^^
into this:
<sc>This is a test</sc> this is <sc>another test</sc>
I got the regex to match them:
\^{2}[^^]+\^{2}
But I'm stuck there. I'm not sure what to do with the other .replace parameter:
.replace(/\^{2}[^^]+\^{2}/g, WHAT_TO_ADD_HERE?)
Any ideas?

You can use replace with regex and grouping like
var text = '^^This is a test^^ this is ^^another test^^'.replace(/\^\^(.*?)\^\^/g, '<sc>$1</sc>')

Here is a piece of code you can use:
var re = /(\^{2})([^^]+)(\^{2})/g;
var str = '^^This is a test^^ this is ^^another test^^\n\n<sc>This is a test</sc> this is <sc>another test</sc>';
var subst = '<sc>$2</sc>';
var result = str.replace(re, subst);
This is just an enhancement of your regex pattern where I added capturing groups. To improve performance and ensure you will be capturing all symbols between the ^^, you can use only one capturing group and . symbol with non-greedy quantificator:
var re = /\^{2}(.+?)\^{2}/g;
Have a look at the example.

In this case you need to use the group index to wrap the content.
var content = "^^This is a test^^ this is ^^another test^^";
content.replace(/\^{2}(.*?)\^{2}/g, '<sc>$1</sc>');
The (.*?) will help you to group the content and in your replace statement use $1 where 1 is the index of group.

Related

Matching patterns not within a set of opening and closing characters e.g {}, ()

I have this string pattern below
str = "nums#1#2#3{#4}#5"
Its there a way I can match all the #\d+ patterns excluding the ones within the curly braces.
I am currently achieving the desired result by replace the curly braces and everything withing them with an empty string before matching.
str = str.replace(/\{[^}]*\}/g, '');
match = str.match(/#\d+/g);
Its there a way to do this in javascript regular expression without the first replacement?
Assuming { and } are balanced, you can use this negative lookahead to match numbers not within {...}:
var str = "nums#1#2#3{#4}#5";
var arr = str.match(/#\d+(?![^{]*})/g)
console.log(arr)
//=> ["#1", "#2", "#3", "#5"]
(?![^{]*} is a negative lookahead that asserts after a number we don't have a } ahead before matching a {
The way is to capture all that you don't want before, example:
var result = txt.replace(/((?:{[^}]*}|[^#{]|#(?!\d))*)(#\d+)/g, '$1 number:$2 ');
Yes, use this one : (?!{)#\d(?!})
Demo
var str = "nums#1#2#3{#4}#5";
var result=str.match(/#\d+(?!})/g);
console.log(result);
you can write like this too.

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. 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. 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')). If the ) is not required at the end, the third solution (replace(/(\/)[^)]+/, '$1textReplaced')) will do. The last solution (s.replace(/\/[^)]+\)/, '/textReplaced)')) will work if the / and ) are static values known beforehand.
You can use 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()
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,
Does notThisText contain any slash /?
Does IWantToReplaceThis contain any parentheses )?
Since there are too many uncertainties, the answer here only shows up the pattern exactly matches your example:
yourText.replace(/(\/).*?(\))/g, '$1textReplaced$2')
var text = "notThisText/IWantToReplaceThis";
text = text.replace(/\/.*/, "/whatever");
output : "notThisText/whatever"`

regex lookbehind in javascript

i im trying to match some words in text
working example (what i want) regex101:
regex = /(?<![a-z])word/g
text = word 1word !word aword
only the first three words will be matched which is what i want to achieve.
but the look behind will not work in javascript :(
so now im trying this regex101:
regex = /(\b|\B)word/g
text = word 1word !word aword
but all words will match and they may not be preceded with an other letter, only with an integer or special characters.
if i use only the smaller "\b" the 1word wont matchand if i only use the "\B" the !word will not match
Edit
The output should be ["word","word","word"]
and the 1 ! must not be included in the match also not in another group, this is because i want to use it with javascript .replace(regex,function(match){}) which should not loop over the 1 and !
The code i use it for
for(var i = 0; i < elements.length; i++){
text = elements[i].innerHTML;
textnew = text.replace(regexp,function(match){
matched = getCrosslink(match)[0];
return "<a href='"+matched.url+"'>"+match+"</a>";
});
elements[i].innerHTML = textnew;
}
Capturing the leading character
It's difficult to know exactly what you want without seeing more output examples, but what about looking for either starts with boundary or starts with a non-letter. Like this for example:
(\bword|[^a-zA-Z]word)
Output: ['word', '1word', '!word']
Here is a working example
Capturing only the "word"
If you only want the "word" part to be captured you can use the following and fetch the 2nd capture group:
(\b|[^a-zA-Z])(word)
Output: ['word', 'word', 'word']
Here is a working example
With replace()
You can use specific capture groups when defining the replace value, so this will work for you (where "new" is the word you want to use):
var regex = /(\b|[^a-zA-Z])(word)/g;
var text = "word 1word !word aword";
text = text.replace(regex, "$1" + "new");
output: "new 1new !new aword"
Here is a working example
If you are using a dedicated function in replace, try this:
textnew = text.replace(regexp,function (allMatch, match1, match2){
matched = getCrosslink(match2)[0];
return "<a href='"+matched.url+"'>"+match2+"</a>";
});
Here is a working example
You can use the following regex
([^a-zA-Z]|\b)(word)
Simply use replace like as
var str = "word 1word !word aword";
str.replace(/([^a-zA-Z]|\b)(word)/g,"$1"+"<a>$2</a>");
Regex

non-capture group still showing in match

I know this topic has been thoroughly covered on StackOverflow, but I can't for the life of me get my regular expression to work. So without further repetitive ado ...
This is what I have.
String: <p model='cat'></p>
Regex: .match(/(?:model=')(.*)(?:')/g)
This is what my expression returns: model='cat'
This is what I want: cat
Why isn't my non capture group ignored? Is it that I don't understand what a non-capturing group does? Why isn't my Regex working?
The entire match will always be group 0, you need to access that specific group (group 1 in this case since the first group is non-capture), you can do it like this:
var str = "<p model='cat'></p>";
var regex = /(?:model=')(.*)(?:')/g
var match = regex.exec(str);
alert(match[1]); // cat
Fiddle
Also, I suppose you are probably wanting several matches within str, you could do that like this:
var str = "<p model='cat'></p><p model='dog'></p><p model='horse'></p>";
var regex = /(?:model=')([^']*)/g
var matches = [];
var match;
while (match = regex.exec(str)) {
matches.push(match[1]);
}
alert(matches); // cat,dog,horse
Fiddle
A non-capturing group is basically just a non-group ― a way to use parentheses without actually treating that part of the pattern as a group.
It looks like what you're actually looking for are the "match prefix but exclude" group (?<=) and the "match suffix but exclude" group (?=).
Note: This type of group does not seem to be supported in Internet Explorer.
If you use these, you get the desired result:
var str = "<p model='cat'></p><p model='dog'></p><p model='horse'></p>";
var regex = /(?<=model=')[^']*(?=')/g
var matches = str.match(regex);
console.log(matches);

How do I make a regular expression that matches everything on a line after a given character?

If I have a String in JavaScript
key=value
How do I make a RegEx that matches key excluding =?
In other words:
var regex = //Regular Expression goes here
regex.exec("key=value")[0]//Should be "key"
How do I make a RegEx that matches value excluding =?
I am using this code to define a language for the Prism syntax highlighter so I do not control the JavaScript code doing the Regular Expression matching nor can I use split.
Well, you could do this:
/^[^=]*/ // anything not containing = at the start of a line
/[^=]*$/ // anything not containing = at the end of a line
It might be better to look into Prism's lookbehind property, and use something like this:
{
'pattern': /(=).*$/,
'lookbehind': true
}
According to the documentation this would cause the = character not to be part of the token this pattern matches.
use this regex (^.+?)=(.+?$)
group 1 contain key
group 2 contain value
but split is better solution
.*=(.*)
This will match anything after =
(.*)=.*
This will match anything before =
Look into greedy vs ungreedy quantifiers if you expect more than one = character.
Edit: as OP has clarified they're using javascript:
var str = "key=value";
var n=str.match(/(.*)=/i)[1]; // before =
var n=str.match(/=(.*)/i)[1]; // after =
var regex = /^[^=]*/;
regex.exec("key=value");

Categories