I'm having following kind of content in my code:
something();_c.log(<ANY CONTENT HERE>);somethingElse();
Now I'd like to have a regexp that returns:
something();somethingElse();
For some reason I can't get this (easily) done. How can I achieve the desired result?
Can you use string replace with regex??
input = 'something();_c.log(<ANY CONTENT HERE>);somethingElse();'
input = input.replace(/(something\(\);).*?(somethingElse\(\);)/g, "$1$2");
Here it is capturing the two groups from your input and replacing everything excepts the two groups($1, $2).
If the something and somethingElse are unknown, and the _c.log is fixed over there, then use this one:
input = input.replace(/(\w+\(\);)_c\.log.*?(\w+\(\);)/g, "$1$2");
input = 'something();_c.log(<ANY CONTENT HERE>);somethingElse();'
input = input.replace(/_c.log\(.*?\);/g, "");
http://jsfiddle.net/2SY3w/
Related
Background: I used quill.js to get some rich text input. The result I want is quite similar to HTML so I went with the quill.container.firstChild.innerHTML approach instead of actually serializing the data. But when it comes to anchor, instead of
Anchor
I actually want
Anchor{{link:test.html}}
With .replace() method I easily got {{link:test.html}}Anchor</a> but I need to put the link description after the Anchor text. Is there a way to swap {{link:test.html}} with the next </a> so I can get the desired result? There can be multiple anchors in the string, like:
str = 'This is a test. And another one here.'
I would like it to become:
str = 'This is a test{{link:test1.html}}. And another one{{link:test2.html}} here.'
You could also use dom methods. The dom is a better html parser than regex. This is a fairly simple replaceWith
str = 'This is a test. And another one here.'
var div = document.createElement('div');
div.innerHTML = str;
div.querySelectorAll('a').forEach(a=>{
a.replaceWith(`${a.textContent}{{link:${a.getAttribute('href')}}}`)
})
console.log(div.innerHTML)
Yes, you can use capture groups and placeholders in the replacement string, provided it really is in exactly the format you've shown:
const str = 'This is a test. And another one here.';
const result = str.replace(/<a href="([^"]+)">([^<]+)<\/a>/g, "$2{{link:$1}}");
console.log(result);
This is very fragile, which is why famously you don't use regular expressions to parse HTML. For instance, it would fail with this input string:
const str = 'This is a test <span>blah</span>. And another one here.';
...because of the <span>blah</span>.
But if the format is as simple and consistent as you appear to be getting from quill.js, you can apply a regular expression to it.
That said, if you're doing this on a browser or otherwise have a DOM parser available to you, use the DOM as charlietfl demonstrates;
I'm trying to clean up a string of text on the server side from the output generated by a wysiwyg. and while I can fix it client side, it's best to also fix this on the server side.
var string = "<p>firstline</p><p>secondline</p><p>thirdline</p><p>iframe</p><p>a</p><p>df</p><p>dsf </p><p><br></p><p>sd</p><p>f</p><p>sdf</p><p><br></p>"
var x = string.replace("<p><br></p>", "");
https://jsfiddle.net/8c0yh9r7/
the code should but doesn't get rid of the break within the paragraphs
why is that?
Use a regex with a global flag, like:
string.replace(/<p><br><\/p>/g, "");
https://jsfiddle.net/Lu2r3820/1/
When using a string only the first occurrence will be replaced.
See replace() documentation
doesn't get rid of the break within the paragraphs
Yes, it does… but only once. You have more than one paragraph containing a line break in your code.
If you want to replace it more than once, you need to use a regex and mark it as global with g.
var x = string.replace(/<p><br><\/p>/g, "");
It does replace, but only the first occurrence. If you run this afterwards, you can see the second occurrence disappearing.
var x = x.replace("<p><br></p>", "");
refer to this to replace all occurrences.
How to replace all occurrences of a string in JavaScript?
So I am trying to figure out how I can remove a select set of characters on the end of a string. I've tried some general 'solutions' like str.replace or creating a rtrim, but I kept seeing some situation in which it wouldn't work.
Possible inputs might be:
\r\n some random text \r\n
\r\n some random text
some random text \r\n
some random text
Only the first and the third line should be affected by this function.
Basicly I'm looking for a rtrim function that takes as a parameter, the value/character set that should be trimmed.
I think it might be something way too obvious that I don't see, but at this point I feel like I could use some help.
You can use the following piece of code to do that for you:
var a = "\r\n some random text \r\n";
a = a.replace(new RegExp('\r\n$'), '');
Here, $ matches end of input.
You can refer to the regular expressions guide here to find out more about regex in JS.
EDIT:
If you really need a function for this:
var rTrimRegex = new RegExp('\r\n$');
var rTrim = function(input){
return input.replace(rTrimRegex, '');
}
And then use it inside your code maybe like:
var str = 'my name is foo\r\n\r\n';
str = rTrim(str);
Trying to do something like making html codes usable in my forum. I want to make text hidden when wrapped with the [hidden] string and after clicking on a button the original text between the [hidden] and [/hidden] tags should be shown. I try using
var res = str.replace("[hidden]$1[/hidden]", "$1");
You need to escape the brackets, [], otherwise they will be interpreted as a character set.
var string = '[hidden]test[/hidden]';
string = string.replace(/\[hidden\](.*?)\[\/hidden\]/g, '$1');
// "test"
I need: www.mydomain.com:1235 form the text var below:
var text = 'http://www.mydomain.com:1235/;image.jpg';
alert(text.match(/\/[^]+\//));
output is: //www.mydomain.com:1235/
How do I exclude the delimiters?
You need to use parens to group what you want to match. Then, the call to .match() will let you use indexers. Index 0 is the whole string match, and index 1 is the first paren grouping.
var text = 'http://www.mydomain.com:1235/;image.jpg';
alert(text.match(/\/([^\/]+)\//)[1]);
Not a regex, but you could do this:
Example: http://jsfiddle.net/nTmv9/
text = text.split('http://')[1].split('/')[0];
or with a regex:
Example: http://jsfiddle.net/nTmv9/1/
text = text.match(/http:\/\/([^\/]+)\//)[1];
This will capture the domain without the http or the url slugs.
https?:\/\/([^\/]+)\/
If you need help figuring out regex here is a great tool I use all of the time.
http://gskinner.com/RegExr/
Cheers