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
Related
I have an probably simply question, even tho I could not find the answer for it via google or Stackoverflow.
I have a very long string which I want to store in a constant, however it looks awfull in my editor:
My objective would be to split this longer string into seperate parts inside my code, somehow like this:
As you see, JS of course does not understand that the line 8-11 should still be part of the string. How can I acomplish that?
You can escape the newlines
const str = `asödfjkdaölksdjaskldjasöldkjakldjadlkajsdEND\
BEGINNINGasdöasjkdaöslkdjasködljasdkljasdlkEND\
BEGINNINGasjköaösdjaöklsdjalkdsjaskld`
console.log(str)
I am currently writing a code-snippet that automatically links certain keywords and saves the links it linked into an array called linked. I do this last step to prevent a certain word to be linked twice.
Now the user is writing into a textbox, writes a keyword it gets linked. That works fine. My problem now is I am trying to handle the situation when he deletes text from the textbox. This means I have to match all links in the text against the linked array and then remove those from the linked array, that aren't in the text anymore. So far the theory. Unfortunately I am stuck with the following error.
Assume we have a text like this:
Test <a href='link1'>Link1</a> <a href='link2'>Link2</a>
I use this regEx (/href='([^\'\"]+)'/g) to get all the hrefs in the text above like so:
var hrefs = $(textInput).val().match(/href='([^\'\"]+)'/g);
This gives me an array that contains the following:
href='link1'
href='link2'
If I start deleting text and end up with something like this:
Test <a href='link1'>Link1</a> <a href='link2
Notice the one ' that is gone, the whole regEx turns out undefined, even though there still is a link in the string. Since I am not an expert with regEx I can't see exactly why? Is there maybe a better regEx for this situation?
You can simplify your regex like this:
/href='[^']+'/g
Demo
http://regex101.com/r/tU2qL0
Use this regex /href=('|")\w+('|")/g like this;
var hrefs = $(textInput).val().match(/href=('|")\w+('|")/g);
This should give you the matches.
BTW, match() is correct. Don't do exec() as #tenub said
Mark it as answer if it helps :)
I am trying to replace a two multiline comments (on a single line) with javascript text in the middle. I am using a build tool, which reads the entire file, and need to replace a specific string (made up of comments) during the build.
Example:
var data = /*testThisDelete:start*/new Date();/*testThisDelete:end*/
Once replaced, should used like this
var data = 4.6.88
Try something like this to get started:
"your file as a string".replace(new RegExp('/\*testThisDelete\:start.*testThisDelete\:end\*/','m'), '"replacement text"');
See this post for a lot of useful additional info: JavaScript replace/regex
Are you looking for:
^.+?(\/\*testThisDelete:start\*\/.+?\/\*testThisDelete:end\*\/)$
With this you should just be able to replace the first matched substring with what you want.
Assume I have the following URL stored in variable called content:
http://www.example.com/watch?v=4444444&feature=related
Problem:
I need to replace watch?v= with embed/
I need to erase whatever comes after &
The final output would look like:
http://www.example.com/embed/4444444
I tried these two steps but didn't work:
content = content.replace('/watch?v=/', 'embed/');
content = content.replace('&*/g','');
The URL in page source code appears as:
http://www.example.com/watch?v=4444444&feature=related
You have many errors:
You are using a regular expression when you only need a string.
You are writing your regular expressions as strings.
To write 'match any characters' you need to write '.*', not just '*'. The star modifies the previous token.
There is no need to use the g flag here.
Try this instead:
content = content.replace('watch?v=', 'embed/').replace(/&.*/, '');
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.