How to remove `//<![CDATA[` and end `//]]>` with javascript from string? - javascript

How to remove //<![CDATA[ and end //]]> with javascript from string?
var title = "<![CDATA[A Survey of Applications of Identity-Based Cryptography in Mobile Ad-Hoc Networks]]>" ;
needs to become
var title = "A Survey of Applications of Identity-Based Cryptography in Mobile Ad-Hoc Networks";
How to do that?

You can use the String.prototype.replace method, like:
title = title.replace("<![CDATA[", "").replace("]]>", "");
This will replace each target substring with nothing. Note that this will only replace the first occurrence of each, and would require a regular expression if you want to remove all matches.
Reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

You ought to be able to do this with a regex. Maybe something like this?:
var myString = "<![CDATA[A Survey of Applications of Identity-Based Cryptography in Mobile Ad-Hoc Networks]]>";
var myRegexp = /<!\[CDATA\[(.*)]]>/;
var match = myRegexp.exec(myString);
alert(match[1]);

I suggest this wider way to remove leading and trailing CDATA stuff :
title.trim().replace(/^(\/\/\s*)?<!\[CDATA\[|(\/\/\s*)?\]\]>$/g, '')
It will also work if CDATA header and footer are commented.

You must perform the OPPOSITE of what was originally done to the string to ENCODE it, which was this:-
mystr='<!CDATA[' + mystr.replaceAll(']]>',']]]]><![CDATA[>') + ']]>'
All the other answers on this page that suggest "replace" without using a loop are wrong.

The regular expresion /^(<!\[CDATA\[)|(]]>)$/gm worked for me without looping.

Related

How would I write a Regular Expression to capture the value between Last Slash and Query String?

Problem:
Extract image file name from CDN address similar to the following:
https://cdnstorage.api.com/v0/b/my-app.com/o/photo%2FB%_2.jpeg?alt=media&token=4e32-a1a2-c48e6c91a2ba
Two-stage Solution:
I am using two regular expressions to retrieve the file name:
var postLastSlashRegEx = /[^\/]+$/,
preQueryRegEx = /^([^?]+)/;
var fileFromURL = urlString.match(postLastSlashRegEx)[0].match(preQueryRegEx)[0];
// fileFromURL = "photo%2FB%_2.jpeg"
Question:
Is there a way I can combine both regular expressions?
I've tried using capture groups, but haven't been able to produce a working solution.
From my comment
You can use a lookahead to find the "?" and use [^/] to match any non-slash characters.
/[^/]+(?=\?)/
To remove the dependency on the URL needing a "?", you can make the lookahead match a question mark or the end of line indicator (represented by $), but make sure the first glob is non-greedy.
/[^/]+?(?=\?|$)/
You don't have to use regex, you can just use split and substr.
var str = "https://cdnstorage.api.com/v0/b/my-app.com/o/photo%2FB%_2.jpeg?alt=media&token=4e32-a1a2-c48e6c91a2ba".split("?")[0];
var fileName = temp.substr(temp.lastIndexOf('/')+1);
but if regex is important to you, then:
str.match(/[^?]*\/([^?]+)/)[1]
The code using the substring method would look like the following -
var fileFromURL = urlString.substring(urlString.lastIndexOf('/') + 1, urlString.lastIndexOf('?'))

Extract characters in URL after certain character up to certain character

I'm trying to extract certain piece of a URL using regex (JavaScript) and having trouble excluding characters after a certain piece. Here's what I have so far:
URL: http://www.somesite.com/state-de
Using url.match(/\/[^\/]+$/)[0] I can extract the state-de like I want.
However when the URL becomes http://www.somesite.com/state-de?page=r and I do the same regex it pulls everything including the "?page=r" which I don't want. I want to only extract the state-de regardless of whats after it (looks like usually a "?" follows it)
This might work:
var arr = url.split("/")
arr[arr.length - 1].split("?")[0]
I'd recommend reading up on regular expressions in general. What you want to do here is make the regular expression stop when it hits the ? in the URL.
Using capturing groups to select which part of the match that you want might also be useful here.
Example:
url.match(/(\/[^\/?]+)(?:\?.*)?$/)[1]
I avoid overly complex RegExs when possible, so I tend to do this in multiple steps (with .replace()):
var stripped = url.replace(/[?#].*/, ''); // Strips anything after ? or #
You can now do the simpler transform to get the state, e.g.:
var state = stripped.split('/').pop()
If you want do it by regex try this one:
url.match(/https?:\/\/([a-z0-9-]+\.)+[a-z]+\/([a-z0-9_-])\/?(\?.*)?/)[1]
Or you could do it using JQuery:
var url = 'http://www.somesite.com/state-de?page=r#mark4';
// Create a special anchor element, set the URL to it
var a = $('<a>', { href:url } )[1];
console.log(a.hostname);
console.log(a.pathname);
console.log(a.search);
console.log(a.hash);

jquery .replace(/./g, "") do not work for me but others

I found this snippet somewhere and it works like a charm:
var n = parseInt(e.find("span.favNum").text().replace(/./g, "")) + 1;
If I do it in a similar way it doesn't work anymore.
I do the following:
<div id ="test">6.987</div>
var test = $("#test");
var r = test.text().replace(/./g, "");
console.log("wrong ", r);
I know that I can replace it also like this:
var r = test.text().replace(".", "");
This works.
I would like to understand why the "stolen" snippet is working.
Any idea?
http://jsfiddle.net/nJZMf/3/
The original script is found here: http://wp-svbtle.themeskult.com/
You will find the snippet by viewing the source of index.html and searching for .replace.
You need to escape the "."
test.text().replace(/\./g, "");
The reason that the code in the page you linked to works, where yours doesn't, is that it's not the same regular expression. Here's what I found in that page (and similar code in several places)
r = n.text().replace( /,/g, "" )
where r is a jQuery object.
Note that the regular expression has a , inside the //, not a . like the code you had trouble with.
Comma is not a special character in regular expressions, so it needs no special treatment. Period has a special meaning. As the other answers pointed out, it matches all characters, and you need to prefix it with \ if you want to match . only.
Also note that .replace() is not jQuery code, it's JavaScript.
jQuery's .text() method returns a JavaScript String value. So anything you do with that string - such as the .replace() call - is actually a JavaScript String method.
The distinction is important when you want to research a problem: a search for "javascript string replace" will get you better information than "jquery replace".
It has to be var r = test.text().replace(/\./g, ""); instead of var r = test.text().replace(/./g, ""); because you need to escape the . in order for it to be replaced.
http://jsfiddle.net/mrk1989/nJZMf/4/
Solution because I add \ in var r = test.text().replace(/\./g, "");
The problem was that you did not escape dot.
But keep in mind that:
.replace(".", "");
.replace(/\./g, "");
are two different things.
For example: https://jsfiddle.net/rmhpkz9n/1/

javascript replace question mark

how to make a regex for ? and = in javascript?
I want something from
http://localhost/search?search=words
to
http://localhost/search/search/words
(?search=) to (/search/)
<script>
var ss = "http://localhost/search?search=words".replace("/\?search\=/g", "/search/");
document.write(ss);
</script>
BTW: just some prastic, not a htaccss rewrite. Thanks.
Almost there! = is not a special character and does not need to be escaped. In addition, regex strings are not wrapped by quotes. So:
"http://localhost/search?search=words".replace(/\?search=/g, "/search/");
How about
str.replace(/[?=]/g, "/");
Do note that it's probably better to make a function to understand the url structure and rebuild it properly, that will produce a much more healthy, robust code, rather then a simple replacement.
You can use a simple string for replace:
var ss = "http://localhost/search?search=words".replace("?search=", "/search/");

How to get IP value from textarea with regular expression

Here is a textarea.(IPv4, Domain)
96.17.109.65 fox.com
And I want to change IP value into another one.
like this,
74.125.71.106 fox.com
I guess it will be like
$('textarea').find('some regular expressions..').val('another one...');
Please help me. I just wanna learn regular expressions.. thanks.
Matching an IP address using regular expression is not as easy as it sounds. There are two methods available:
simple, but can lead to false-positives:
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
complex, but always correct:
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
If you want to replace it, you'll need something like this (using the simple RegExp as an example):
var textarea = $('textarea');
textarea.val(textarea.val().replace(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g, '74.125.71.106'));
This uses a basic regex to find the seperator between ip and domain, it doesn't validate but it should do want you need:
var newIP = '74.125.71.106';
var $t = $('textarea');
$t.val($t.val().replace(/^([^\s]+)/mg, newIP + ' '));

Categories

Resources