I'm having a trouble with blog posts excerpt..
I'm using Wordpress as headless CMS and it returns me post excerpt in specific format.
It looks like <p>some text here [...]</p> and I'm trying to write one regular expression that will get rid of paragraphs and those brackets with dots in between.
I ended up with something like
excerpt.replace(/<p>|<\/p>/g, '') and it works with paragraphs but I can't find any solution to get rid of those three dots in one regular expression..
Is that possible at all?
Ok - I didn't notice one thing xD
Wordpress returns […] instead of [...]..
Now everything works perfectly with something like
const parsedExcerpt = excerpt.replace(/<p>|<\/p>|\r?\n|\r|\[…]/g, '');
Related
I am building a graph drawer and currently working on the math expression parser. I'm done with most parts but I'm stuck at clearing the input text before parsing it. What I'm trying to achieve now is getting rid of unpermitted characters.
For example, in this text:
5ax+4asxxv+sdflog10aloga(132*43)sin(132)
I want to match everything that is not +,-,*,/,^,(,),ln,log,sin,cos,tan,cot,arcsin,arccos,...
and replace them with "".
so that the output is
5x+4xx+log10log(132*43)sin(132)
I need help with the regex.
Spaces don't matter since I clear them out beforehand.
A little bit tricky - at least I couldn't think of a simple way to do what you ask. The regex would get monstrous.
So I did it the other way around - match what you want to keep, and put it back together.
The regex:
[\d+*/^()x-]|ln|log|(?:arc)?(?:sin|cos)|tan|cot
The code:
var re = /[\d+*/^()x-]|ln|log|(?:arc)?(?:sin|cos)|tan|cot/g,
text = '5ax+4asxxv+sdflog10aloga(132*43)sin(132)arccos(1)';
console.log(text.match(re).join(''));
I know it's going to be a VERY obvious answer, but I can't find anything on how to do this.
I'm trying to unescape < and > within an HTML string
My test output string is essentially:
```php
>h2<Heading2>/h2<
```
`>h2<Heading2>/h2<`
>h2<Heading2>/h2<
So in this example we have Github flavoured Markdown, a regular code markdown snippet, and then raw text all with the same HTML tag. I want to unescape the raw tag (the third one) to actually become a link. The ideal output would be something like this.
```php
>h2<Heading2>/h2<
```
`>h2<Heading2>/h2<`
<h2>Heading2</h2>
I'm getting stuck at getting multiple > in the same line.
Current regex:
/(?:.*?(>))/
This will get the first entry.
/(?:.*?(>))/g
This one gets the second entry. I want it to be able to get EVERY entry. Then, it's just a matter of throwing the tick pieces.
/(?:```|`)(?:.*?(>)).*?(?:```|`)/gs
If you're intending on using a regular expression for this task, you can consider the following:
var r = s.replace(/((`(?:``)?)[^`]*\2)|>/g, '$1<')
.replace(/((`(?:``)?)[^`]*\2)|</g, '$1>')
.replace(/`[<>]+/g, '`');
Working Demo
I'm trying to make a regex to capture all endings in:
hello://a.b.c/d/
that is, hello://a.b.c/d/.*
except for "Main_Page" ending, that is:
hello://a.b.c/d/Main_Page
I tried using this answer but when I try my string, the dots between a and b seem to get in the way, so that something like:
hello://a.b.c/d/(?!Main_Page)([-\w]*\.)(?!Main_Page)([-\w]*\.)(?!Main_Page)\S*
does not work. So as an example, I would like to match to the first 2 lines and not the third below:
hello://a.b.c/d/Running
hello://a.b.c/d/Maine
hello://a.b.c/d/Main_Page
This seems to work just fine:
/hello:\/\/a\.b\.c\/d\/(?!Main_Page).*/g
Online Demo
I am using Sublime Text to write some Javascript and need to do a simple text replacement in the editor in order to set code up. I can do it manually but I figured there must be a way to have the replacement occur automatically with RegEx. I've used RegEx a bunch before but have never used it to grab data from one part of the code to reference and edit another part of the code. For example, I have this:
var example_1 = 836;
var example_2 = 837;
var example_3 = 838;
var example_4 = 846;
And then I have this:
SELECT_122=836
SELECT_143=837
SELECT_144=838
SELECT_145=846
I want these to use the corresponding values and format them like this:
SELECT_122: example_1,
SELECT_143: example_2,
SELECT_144: example_3,
SELECT_145: example_4
Note that I'm updating the equal signs to colons with spaces so I figured doing all these changes could be done with some sort of search and replace. I have a large amount of these so I figured it would be best to learn how to do this if it's possible.
I don't have SublimeText, but you said in a comment that you want to do it through a text editor. Here is what works for me in EditPad Pro, it may work in Sublime.
Search:
(?s)(var (example_\d++) = (\d++).*?SELECT_\d++)=\3
Replace:
\1: \2,
Then I click "Replace". This will replace the first instance (SELECT_122=836) with "SELECT_122: example_1,"
Then I click "Replace Next" multiple times, and the SELECT_ strings are left looking like this:
SELECT_122: example_1,
SELECT_143: example_2,
SELECT_144: example_3,
SELECT_145: example_4,
Is this what you want?
Hope the regex and replacement string at least get you started. :)
I have something like the following;-
<--customMarker>Test1<--/customMarker>
<--customMarker key='myKEY'>Test2<--/customMarker>
<--customMarker>Test3 <--customInnerMarker>Test4<--/customInnerMarker> <--/customMarker>
I need to be able to replace text between the customMarker tags, I tried the following;-
str.replace(/<--customMarker>(.*?)<--\/customMarker>/g, 'item Replaced')
which works ok. I would like to also ignore custom inner tags and not match or replace them with text.
Also I need a separate expression to extract the value of the attribute key='myKEY' from the tag with Text2.
Many thanks
EDIT
actually I am trying to find things between comment tags but the comment tags were not displaying correctly so I had to remove the '!'. There's a unique situation that required comment tags... in anycase if anyone knows enough regex to help, it would be great. thank u.
In the end, I did something like the following (incase anyone else needs this. enjoy!!! But note: Word about town is that using regex with html tags is not ideal, so do your own research and make up your mind. For me, it had to be done this way, mostly bcos i wanted to, but also bcos it simplified the job in this instance);-
var retVal = str.replace(/<--customMarker>(.*?)<--\/customMarker>/g, function(token, match){
//question 1: I would like to also ignore custom inner tags and not match or replace them with text.
//answer:
var replacePattern = /<--customInnerMarker*?(.*?)<--\/customInnerMarker-->/g;
//remove inner tags from match
match = $.trim(match.replace(replacePattern, ''));
//replace and return what is left with a required value
return token.replace(match, objParams[match]);
//question 2: Also I need a separate expression to extract the value of the attribute key='myKEY' from the tag with Text2.
//answer
var attrPattern = /\w+\s*=\s*".*?"/g;
attrMatches = token.match(attrPattern);//returns a list of attributes as name/value pairs in an array
})
Can't you use <customMarker> instead? Then you can just use getElementsByTagName('customMarker') and get the inner text and child elements from it.
A regex merely matches an item. Once you have said match, it is up to you what you do with it. This is part of the problem most people have with using regular expressions, they try and combine the three different steps. The regex match is just the first step.
What you are asking for will not be possible with a single regex. You're going to need a mini state machine if you want to use regular expressions. That is, a logic wrapper around the matches such that it moves through each logical portion.
I would advise you look in the standard api for a prebuilt engine to parse html, rather than rolling your own. If you do need to do so, read the flex manual to get a basic understanding of how regular expressions work, and the state machines you build with them. The best example would be the section on matching multiline c comments.