How can i remove http://webservices.rki.dk this in xml - javascript

I want to remove namespace in xml.Can you please write regular expression in javascript for the following two strings.I want these two strings in my whole xml.
xmlns="http://webservices.rki.dk"
xmlns="http://webservices.rki.dk/"

You want to use string.replace() to get rid of those. See here.
Example to globally replace in a string (You need to escape some chars to make it work):
var str1 = 'xmlns="http://webservices.rki.dk" hello xmlns="http://webservices.rki.dk"'
var str1Fixed = str1.replace(/xmlns=\"http:\/\/webservices.rki.dk\"/g,"");
alert(str1Fixed);

Related

How do I replace string within quotes in javascript?

I have this in a javascript/jQuery string (This string is grabbed from an html ($('#shortcode')) elements value which could be changed if user clicks some buttons)
[csvtohtml_create include_rows="1-10"
debug_mode="no" source_type="visualizer_plugin" path="map"
source_files="bundeslander_staple.csv" include cols="1,2,4" exclude cols="3"]
In a textbox (named incl_sc) I have the value:
include cols="2,4"
I want to replace include_cols="1,2,4" from the above string with the value from the textbox.
so basically:
How do I replace include_cols values here? (include_cols="2,4" instead of include_cols="1,2,4") I'm great at many things but regex is not one of them. I guess regex is the thing to use here?
I'm trying this:
var s = $('#shortcode').html();
//I want to replace include cols="1,2,4" exclude cols="3"
//with include_cols="1,2" exclude_cols="3" for example
s.replace('/([include="])[^]*?\1/g', incl_sc.val() );
but I don't get any replacement at all (the string s is same string as $("#shortcode").html(). Obviously I'm doing something really dumb. Please help :-)
In short what you will need is
s.replace(/include cols="[^"]+"/g, incl_sc.val());
There were a couple problems with your code,
To use a regex with String.prototype.replace, you must pass a regex as the first argument, but you were actually passing a string.
This is a regex literal /regex/ while this isn't '/actually a string/'
In the text you supplied in your question include_cols is written as include cols (with a space)
And your regex was formed wrong. I recomend testing them in this website, where you can also learn more if you want.
The code above will replace the part include cols="1,2,3" by whatever is in the textarea, regardless of whats between the quotes (as long it doesn't contain another quote).
First of all I think you need to remove the quotes and fix a little bit the regex.
const r = /(include_cols=\")(.*)(\")/g;
s.replace(r, `$1${incl_sc.val()}$3`)
Basically, I group the first and last part in order to include them at the end of the replacement. You can also avoid create the first and last group and put it literally in the last argument of the replace function, like this:
const r = /include_cols=\"(.*)\"/g;
s.replace(r, `include_cols="${incl_sc.val()}"`)

JavaScript RegEx match unless wrapped with [nocode][/nocode] tags

My current code is:
var user_pattern = this.settings.tag;
user_pattern = user_pattern.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); // escape regex
var pattern = new RegExp(user_pattern.replace(/%USERNAME%/i, "(\\S+)"), "ig");
Where this.settings.tag is a string such as "[user=%USERNAME%]" or "#%USERNAME%". The code uses pattern.exec(str) to find any username in the corresponding tag and works perfectly fine. For example, if str = "Hello, [user=test]" then pattern.exec(str) will find test.
This works fine, but I want to be able to stop it from matching if the string is wrapped in [nocode][/nocode] tags. For example, if str = "[nocode]Hello, [user=test], how are you?[/nocode]" thenpattern.exec(str)` should not match anything.
I'm not quite sure where to start. I tried using a (?![nocode]) before and after the pattern, but to no avail. Any help would be great.
I would just test if the string starts with [nocode] first:
/^\[nocode\]/.test('[nocode]');
Then simply do not process it.
Maybe filter out [nocode] before trying to find the username(s)?
pattern.exec(str.replace(/\[nocode\](.*)\[\/nocode\]/g,''));
I know this isn't exactly what you asked for because now you have to use two separate regular expressions, however code readability is important too and doing it this way is definitely better in that aspect. Hope this helps 😉
JSFiddle: http://jsfiddle.net/1f485Lda/1/
It's based on this: Regular Expression to get a string between two strings in Javascript

Extracting HTML string within XML tag with jQuery

I've been working at this for a week and I'm stumped.
I'm trying to parse an RSS feed from SharePoint using jQuery. Using $.find works great on extracting the data between valid XML tags in the feed, but unfortunately one of the tags stores several HTML tags instead of the nice and clean strings like the others.
I have the tag extracted and stored as a string using the following:
$(xml).find("item").each(function () {
var description = $(this).find('description').text();
})
Which gives me the contents of the description tag:
<![CDATA[<div><b>Title:</b> Welcome!</div>
<div><b>Modified:</b> 6/10/2014 7:58 AM</div>
<div><b>Created:</b> 6/3/2014 2:55 PM</div>
<div><b>Created By:</b> John Smith</div>
<div><b>Modified By:</b> Samuel Smith</div>
<div><b>Version:</b> 1.0</div>
<div><b>AlertContent:</b> Stop the presses.</div>
<div><b>Team:</b> USA.</div>]]>
Now my problem is extracting and storing the useful bits. Is there a way to only extract the text following AlertContent:</b>? It seems this might be possible using regular expressions, but I don't know how to make a filter that would start at the end of the bold tag and extend all the way until the start of the closing div tag. Or is there a better way through jQuery's methods?
Sure you're quite right; regular expressions can help you do that. Here is how you can do it:
var alertContent = description.replace(/^.*AlertContent:</b>([^<]*).*$/i, '$1');
WORKING JSFIDDLE DEMO
I'm sure you've heard the warnings about parsing xml with regex. Nevertheless, in case you'd like to know how to do it with regex, this simple pattern will do it:
AlertContent:<\/b>([^<]*)
We start by matching AlertContent:</b>
Then the negative character class [^<]* matches all characters that are not a < and the parentheses capture them to Group 1
All we need to do is read Group 1. Here is sample code to do it:
var regex = /AlertContent:<\/b>([^<]*)/;
var match = regex.exec(string);
if (match != null) {
alert = match[1];
}

Splitting a path using containing \ and / using a RegExp

I want to split a path containing forward and/or backward slashes into parts using Google Apps script.
I prefer doing this by using a single RegExp instead of writing much code.
so code should look like
var myPath = "<something>"; // i.e. \first\second/third\filename.extension
var parts = myPath.replace(<a regular expression>).split('/');
Allthough I searched SO, I didn't find a solution.
What should <a regular expression> be?
If you're just going to split, there's no need to replace beforehand then split. You can pass the regex to the split function directly. Like this:
var parts = mPath.split(/[/\\]/);

Get part of string?

I have a string formatted like this:
item_questions_attributes_abc123_id
I'm specifically trying to get the abc123 bit. That string can be any alphanumeric of any case. No special characters or spaces.
I'm using jQuery, though I'm certainly fine with using straight javascript if that's the best solution.
If it's always the 4th part of the string you can use split.
var original = 'item_questions_attributes_abc123_id';
var result = original.split('_')[3];
Try this:
var myArray = myString.split("_");
alert(myArray[3]);
use split method of javascript and then use 2nd last index you will have your required data.

Categories

Resources