I am hoping that someone can help me. I am not exactly sure how to use the following regex. I am using classic ASP with Javascript
completehtml = completehtml.replace(/\<\!-- start-code-remove --\>.*?\<\!-- start-code-end --\>/ig, '');
I have this code to remove everything between
<\!-- start-code-remove --\> and <\!-- start-code-end --\>
It works perfect up to the point where there is line breaks in the values between start and end code...
How will I write the regex to remove everything between start and end even if there is line breaks
Thanks a million for responding...
Shoud I use the \n and \s characters not 100% sure..
(/\<\!-- start-code-remove --\>\s\n.*?\s\n\<\!-- start-code-end --\>/ig, '');
also the code should not be greedy between <\!-- start-code-remove --\> <\!-- start-code-end --\>/ and capture the values in groups...
There could be 3 or more of these sets...
The dot doesn't match new lines in Javascript, nor is there a modifier to make it do that (unlike in most modern regex engines). A common work-around is to use this character class in place of the dot: [\s\S]. So your regex becomes:
completehtml = completehtml.replace(
/\<\!-- start-code-remove --\>[\s\S]*?\<\!-- start-code-end --\>/ig, '');
Try (.|\n|\r)*.
completehtml = completehtml.replace(/\<\!-- start-code-remove --\>(.|\n|\r)*?\<\!-- start-code-end --\>/ig, '');
Source
There is indeed no /s modifier to make the dot match all characters, including line breaks. To match absolutely any character, you can use character class that contains a shorthand class and its negated version, such as [\s\S].
Regex support in javascript is not very reliable.
function remove_tag_from_text(text, begin_tag, end_tag) {
var tmp = text.split(begin_tag);
while(tmp.length > 1) {
var before = tmp.shift();
var after = tmp.join(begin_tag).split(end_tag);
after.shift();
text = before + after.join(end_tag);
tmp = text.split(begin_tag);
}
return text;
}
Related
I'm trying to replace everything between special characters of a string in Javascript.
var text = "Hello,\n>> Someone lalalala\nMore Text\n<<";
I've tried the following code:
var newText = text.replace(/>>.*<</, ">>Some other text<<");
But at the end it actually returns the text variable.
I'd appreciate some thoughts about this. Thanks.
Regexes are "greedy", meaning they'll try to match the longest substring possible. Since .* means literally any character, it's going to include your delimiter << as well. Thus, .* reaches all the way to the end of your string, and then it can't find << after that, so the match will fail. You have to exclude it in your expression:
text.replace(/>>[^<]*<</, ">>Some other text<<");
The problem is that '.' does not match new lines. Using this answer:
var text = "Hello,\n>> Someone lalalala\nMore Text\n<<";
var newText = text.replace(/>>[\s\S]*<</m, ">>Some other text<<");
console.log(newText);
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"`
I'm trying to remove all ocurrences of dashes and underscores of a string with String.prototype.replace(), but it's not working and I don't know why. My code:
var str = "dash-and_underscore";
str = str.replace(/_|\-/, " ");
console.log(str);
outputs:
"dash and_underscore"
in the Chrome console.
Since the | acts like the OR opperator, what am I doing wrong? I've tried the solution here, but it didn't work, or I'm too dumb to understand - which is an option ;)
Try this:
str = str.replace(/[_-]/g, " ");
[..] defines a character class
g means global research
(You can write it with a quantifier /[_-]+/g to remove several consecutive characters at a time.)
or
str = str.replace(/_|-/g, " ");
that is correct too, but slower. Note that the dash doesn't need to be escaped out of a character class since it isn't a special character.
I have a string like that:
var str = 'aaaaaa, bbbbbb, ccccc, ddddddd, eeeeee ';
My goal is to delete the last space in the string. I would use,
str.split(0,1);
But if there is no space after the last character in the string, this will delete the last character of the string instead.
I would like to use
str.replace("regex",'');
I am beginner in RegEx, any help is appreciated.
Thank you very much.
Do a google search for "javascript trim" and you will find many different solutions.
Here is a simple one:
trimmedstr = str.replace(/\s+$/, '');
When you need to remove all spaces at the end:
str.replace(/\s*$/,'');
When you need to remove one space at the end:
str.replace(/\s?$/,'');
\s means not only space but space-like characters; for example tab.
If you use jQuery, you can use the trim function also:
str = $.trim(str);
But trim removes spaces not only at the end of the string, at the beginning also.
Seems you need a trimRight function. its not available until Javascript 1.8.1. Before that you can use prototyping techniques.
String.prototype.trimRight=function(){return this.replace(/\s+$/,'');}
// Now call it on any string.
var a = "a string ";
a = a.trimRight();
See more on Trim string in JavaScript? And the compatibility list
You can use this code to remove a single trailing space:
.replace(/ $/, "");
To remove all trailing spaces:
.replace(/ +$/, "");
The $ matches the end of input in normal mode (it matches the end of a line in multiline mode).
Try the regex ( +)$ since $ in regex matches the end of the string. This will strip all whitespace from the end of the string.
Some programs have a strip function to do the same, I do not believe the stadard Javascript library has this functionality.
Regex Reference Sheet
Working example:
var str = "Hello World ";
var ans = str.replace(/(^[\s]+|[\s]+$)/g, '');
alert(str.length+" "+ ans.length);
Fast forward to 2021,
The trimEnd() function is meant exactly for this!
It will remove all whitespaces (including spaces, tabs, new line characters) from the end of the string.
According to the official docs, it is supported in every major browser. Only IE is unsupported. (And lets be honest, you shouldn't care about IE given that microsoft itself has dropped support for IE in Aug 2021!)
I have a textbox where a user puts a string like this:
"hello world! I think that __i__ am awesome (yes I am!)"
I need to create a correct URL like this:
hello-world-i-think-that-i-am-awesome-yes-i-am
How can it be done using regular expressions?
Also, is it possible to do it with Greek (for example)?
"Γεια σου κόσμε"
turns to
geia-sou-kosme
In other programming languages (Python/Ruby) I am using a translation array. Should I do the same here?
Try this:
function doDashes(str) {
var re = /[^a-z0-9]+/gi; // global and case insensitive matching of non-char/non-numeric
var re2 = /^-*|-*$/g; // get rid of any leading/trailing dashes
str = str.replace(re, '-'); // perform the 1st regexp
return str.replace(re2, '').toLowerCase(); // ..aaand the second + return lowercased result
}
console.log(doDashes("hello world! I think that __i__ am awesome (yes I am!)"));
// => hello-world-I-think-that-i-am-awesome-yes-I-am
As for the greek characters, yeah I can't think of anything else than some sort of lookup table used by another regexp.
Edit, here's the oneliner version:
Edit, added toLowerCase():
Edit, embarrassing fix to the trailing regexp:
function doDashes2(str) {
return str.replace(/[^a-z0-9]+/gi, '-').replace(/^-*|-*$/g, '').toLowerCase();
}
A simple regex for doing this job is matching all "non-word" characters, and replace them with a -. But before matching this regex, convert the string to lowercase. This alone is not fool proof, since a dash on the end may be possible.
[^a-z]+
Thus, after the replacement; you can trim the dashes (from the front and the back) using this regex:
^-+|-+$
You'd have to create greek-to-latin glyps translation yourself, regex can't help you there. Using a translation array is a good idea.
I can't really say for Greek characters, but for the first example, a simple:
/[^a-zA-Z]+/
Will do the trick when using it as your pattern, and replacing the matches with a "-"
As per the Greek characters, I'd suggest using an array with all the "character translations", and then adding it's values to the regular expression.
To roughly build the url you would need something like this.
var textbox = "hello world! I think that __i__ am awesome (yes I am!)";
var url = textbox.toLowerCase().replace(/([^a-z])/, '').replace(/\s+/, " ").replace(/\s/, '-');
It simply removes all non-alpha characters, removes double spacing, and then replaces all space chars with a dash.
You could use another regular expression to replace the greek characters with english characters.