so I need to replace 2 sets of words in a string; the title of the web page. However, I seem to be able to get one set of words to be removed.
The title is being created by wordpress, which is adding words at the start and end of the title which I don't want to be displayed (as I am calling the title, using PHP, to dynamically create a few bits of information on the page, which are subject to change)
The code I have so far is:
<script>
var str = document.title.replace(/ - CompanyName/i, '');
document.write(str);
</script>
However, I need something which is basically:
var str = document.title.replace(/ - CompanyName/i, '') && document.title.replace(/The /i, '');
This is because the title will produce itself like "The PAGETITLE - CompanyName"
Any ideas how to remove 2 sections of the same string?
Keep the title in a separate variable, re-assign the variable with the result of each replace, set the document title:
var title = "The PAGETITLE - CompanyName";
title = title.replace("The ", "");
title = title.replace(" - CompanyName", "");
document.title = title;
Or, if you like one-liners:
document.title = document.title.replace("The ", "").replace(" - CompanyName", "");
you can directly use like this
var title = "The PAGETITLE - CompanyName";
title.replace(/The(.*?)-[^-]*/,'$1')
var str = document.title.replace(/ - CompanyName/i, '').replace(/The
/i, '');
the replace function yields the new string, which you want to run through replace again.
You can chain functions in JQuery like this:
var str = document.title.replace(/ - CompanyName/i, '').replace(/The /i, '');
Related
i need to fetch the text after / from the below code.How to achieve this one.I tried to find '/' location or index value by search() but it didn't worked.The scenario is i need to fetch the text which consists 2 parts as 'Username / some text'. Where user name is dynamic and i need text after the '/'
in HTML it looks like this (for one instance)
<a id="open_0" class="search-result-heading" data-gistname="15b6402d51d897f2ed29" "="" href="http://127.0.0.1:8080/edit.html?notebook=15b6402d51d897f2ed29">tejas1493 / ZZZZ</a>
i able to fetchText for the above but after fetching i need the ' / ZZZZ ' text
Try this one...it might work. output will be ZZZZ.
path = "path of the selector";
var pos = path.lastIndexOf("/");
var name = path.substring(pos+1 , date.length-1);
You can make it without regular expressions with a simple search as you described :
var text = document.getElementById('open_0').innerHtml;
var slash_index = text.search("/");
var first_word = text.substring(0, slash_index);
var second_word = text.substring(slash_index+1);
Running the below code will give you the contents in place of ZZZZ where Z can be a digit, uppercase letter or a lowercase letter.
raw_text = document.getElementById('open_0').innerHTML;
text = raw_text.match(/\/[ ]*[0-9a-zA-Z]+/)[0].match(/[0-9a-zA-Z]+/)[0];
alert(text);
Output: ZZZZ
NOTE: It will show the string all the way to the end of the text within that tag and not just 4 characters (if there are more characters).
I am trying to "clean" a text string that looks something like this:
DATABASE:madsat NL:Show all platforms of Salute generated from NAIs with no go mobility.
The cleaned string should look like this:
Show all platforms of Salute generated from NAIs with no go mobility.
I am trying the following code but it doesn't seem to like it when I pass in a variable as the string gets returned unchanged:
$(document).ready(function(){
$.get('inputQueryExamples.txt',function(data){
var queryString = data;
var cleanString = "";
var db = '';
$('#database-list').change(function(){
db = $('#database-list').val();
// /(^DATABASE:.*\r\n)(^NL.*)/gm
// http://regex101.com/r/mN4hS2
regex = new RegExp('(^DATABASE:'+ db +'\r\n)(^NL.*)' ,'gm');
console.log(db);
console.log(regex);
//put code in here to replace un-needed stuff
$('#what').append(regex + '<br>');
cleanString = queryString.match(regex);
var nlString = cleanString.map(function(el) {return el.replace('DATABASE:' + db + ' NL:','');});
for (i=0; i<nlString.length; i++){
$('#what').append(nlString[i]+'<br>');
}
}); // end change
Any insight into what i am doing wrong will be appreciated. Thanks
So this works, but I am not sure why I have to process the string twice. Can anyone explain?
var nlString = cleanString.map(function(el) {return el.replace('DATABASE:' + db,'');});
nlString = nlString.map(function(el){return el.replace('NL:',''); });
Something like this?
var s = "DATABASE:madsat \r\nNL:Show all platforms of Salute generated from NAIs with no go mobility.";
var db = "madsat";
var r = new RegExp('(^DATABASE:'+ db + '[\\s\\r\\n]*)(^NL:)' ,'gm');
s1.replace(r, "");
//=> "Show all platforms of Salute generated from NAIs with no go mobility."
Update
It took a while for what you're trying to do to sink in (and your sample data was pretty buried in that regex101 link.)
But I think this JSBin is something close to what you want to do: It still does one pass to find the matches, and a second one to remove the unwanted parts of that match. But the second pass is handled by a single regex replace call rather than your double replaces above. (Click the "Run with JS" button and enter "madsat" or "geoquery" in the box.)
This is the relevant code:
$(document).ready(function() {
$.ajax('http://jsbin.com/fase/1.js', {dataType:'text'}).done(function(data){
var $what = $("#what");
$('#database-list').change(function(){
var db = $(this).val(),
base = '(^DATABASE:'+ db + '[\\s\\r\\n]*)(^NL:)';
var regex1 = new RegExp(base + '(.*)' ,'gm');
var regex2 = new RegExp(base, 'gm');
(data.match(regex1) || []).map(function(str) {
return str.replace(regex2, "");
}).forEach(function(query) {
$what.append(query + "<br/>");
});
});
});
});
Note that the two regexes are identical except that the first one matches the remainder of the "NL:"-line, and the second one doesn't.
I'm trying to find a specific character, for example '?' and then remove all text behind the char until I hit a whitespace.
So that:
var string = '?What is going on here?';
Then the new string would be: 'is going on here';
I have been using this:
var mod_content = content.substring(content.indexOf(' ') + 1);
But this is not valid anymore, since the specific string also can be in the middle of a string also.
I haven't really tried anything but this. I have no idea at all how to do it.
use:
string = string.replace(/\?\S*\s+/g, '');
Update:
If want to remove the last ? too, then use
string = string.replace(/\?\S*\s*/g, '');
var firstBit = str.split("?");
var bityouWant = firstBit.substring(firstBit.indexOf(' ') + 1);
I have a JQuery function that grabs the URL path and adds it as a body class as such:
var pathname = window.location.pathname;
var pathSlashesReplaced = pathname.replace(/\//g, " ");
var pathSlashesReplacedNoFirstDash = pathSlashesReplaced.replace(" ","");
var newClass = pathSlashesReplacedNoFirstDash.replace(/\(\d*\)/g, '').replace(/\s/, '-');
$("body").addClass(newClass);
if ( $("body").attr("class") == "")
{
$("body").addClass("class");
}
.. so if the url is something like /myapp/user/list, the body class ends up as:
<body class="myapp-user list">
The issue is I would like to have all three words with dashes so it should be:
<body class="myapp-user-list">
.. and then I can theme using the CSS:
.myapp-user-list {
}
I am pretty sure there is an issue with my RegEx but I cannot figure out where. I tried various text functions but then the problem was that it grabbed all the text from the page and put that in as the body class:
var text = $(this).text();
var newClass = $.trim(text.replace(/\(\d*\)/g, '').toLowerCase()).replace(/\s/, '-');
Try :
var newClass = location.pathname.split('/').join('-').replace(/(^-|-$)/g,''),
$body = $("body").addClass(newClass);
if ($body.attr("class") == "") {
$body.addClass("class");
}
I would avoid the class name class. It could give problems.
Try adding g modifier to the second replace to replace all white spaces and not just the first.
var newClass = pathSlashesReplacedNoFirstDash.replace(/\(\d*\)/g, '').replace(/\s/g, '-');
I think that applying these two regexps will do the job.
"/\//-/g" Will change all slashes to -
"/-//" Will only remove the first -
Here is the Original string :
var str = " a vartiable";
and I need this part:
str = "https://sjobs.brassring.com/1033/ASP/TG/cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&jobId=385594&type=hotjobs&JobReqLang=141&JobSiteId=5239&JobSiteInfo=385594_5239&GQId=0";
In other words, I need to remove the tag <a> and the document.href value
Thanks guys.
How about:
var str = " a vartiable";
str.replace(/^<a href="(https.*?)cim_home\.asp.*?'(cim_jobdetail\.asp.*)'.*$/, "$1$2");
produces:
"https://sjobs.brassring.com/1033/ASP/TG/cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&jobId=385594&type=hotjobs&JobReqLang=141&JobSiteId=5239&JobSiteInfo=385594_5239&GQId=0"
Something simple like the following should work...
href="(.*?)"
here's the code u want:
var str = ' a vartiable'
var url = /\"(.*?)\"/str
that's how you match, here's how you strip it out:
str.replace(/\"(.*?)\"/, "$1");
the \"(.*?)\" gives the first minimal set of characters between two " characters the id of $1 then the second argument to the replace function tells it to replace the whole string with what's contained in $1
Also, if you use jQuery, this becomes pretty trivial:
var url = $("a").attr("href");