any ideas??
I have this code in hundreds of pages I need to remove it from all these pages but because of the " var s " where everypage is a different 4 digit number i cant just do a find and replace replace with nothing
.
How can I create a powershell command using regex on those four digits.
<script language="javascript" type="text/javascript">
var d = '<%=joindomain%>';
var s = '9244';
var a = '<%=Request.QueryString("aff") %>';
var c = '<%=Request.QueryString("camp") %>';
var r = '<%=referer %>';
</script>
Thank you
Rico
Not sure how general you can be here... Code below may do more than you ask for, if that is the case - just add more things to disambiguate it. Anyway, it did the job for me... ;)
#'
foo
<script language="javascript" type="text/javascript">
var d = '<%=joindomain%>';
var s = '9244';
var a = '<%=Request.QueryString("aff") %>';
var c = '<%=Request.QueryString("camp") %>';
var r = '<%=referer %>';
</script>
bar
'# -replace '<script language[\s\S]*?var s = ''\d{4}''[\s\S]*?</script>'
it will remove any piece of text between "" that contains string "var s = '####'" (4 digits). Notice that I would have to escape things for regex and single quotes for it to work. At some point you may consider [Regex]::Escape method handy, if you want to be very specific...
Related
In my app, I'm trying to split a string into an array based on a regex pattern. I'd like to be able to load my volt templates and run them through our custom rendering engine - just to learn a bit more on how rendering engines work.
I wrote the regex below to do just that:
"(?s)(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})"
And this is an example of such a template:
# {{ title }}
{{created_at}} {{created_location}}
============
Paragraphs are separated by a blank line.
2nd paragraph. *Italic*, **bold**, and `monospace`.
Itemized lists look like:
{% for (item in items) %}
* {{ item }}
{% endfor %}
Now, ideally, I'd like this to be converted to an array looking like this:
[
"# ",
"{{ title }}",
"\n",
"{{created_at}}",
" ",
"{{created_location}}",
"\n============\nParagraphs are separated by a blank line\n2nd paragraph. *Italic*, **bold**, and `monospace`.\n\nItemized lists look like:"
"{% for (item in items) %}",
"\n* {{ item }}\n",
"{% endfor %}"
]
However, when I run the regex above, I get:
[
"Paragraphs are separated by a blank line.\n2nd paragraph. *Italic*, **bold**, and `monospace`.\n\nItemized lists look like:",
"{% for (item in items) %}\n* {{ item }}",
"{% endfor %}\n"
]
As you can see the title part completely disappears. Furthermore, there seem to be some issues with the newline characters. Any ideas how I could solve this?
The problem wasn't in the regex, but in the code that I was using to split on the regex. I modified the code below to also return the regex itself.
extension NSRegularExpression {
func split(_ str: String) -> [String] {
let range = NSRange(location: 0, length: str.characters.count)
//get locations of matches
var matchingRanges: [NSRange] = []
let matches: [NSTextCheckingResult] = self.matches(in: str, options: [], range: range)
for match: NSTextCheckingResult in matches {
matchingRanges.append(match.range)
}
//invert ranges - get ranges of non-matched pieces
var pieceRanges: [NSRange] = []
//add first range
pieceRanges.append(NSRange(location: 0, length: (matchingRanges.count == 0 ? str.characters.count : matchingRanges[0].location)))
var endLoc: Int = 0
var startLoc: Int = 0
//add between splits ranges and last range
for i in 0..<matchingRanges.count {
let isLast = i + 1 == matchingRanges.count
let location = matchingRanges[i].location
let length = matchingRanges[i].length
startLoc = location + length
endLoc = isLast ? str.characters.count : matchingRanges[i + 1].location
pieceRanges.append(NSRange(location: startLoc, length: endLoc - startLoc))
}
var pieces: [String] = []
var previous: NSRange = NSRange(location: 0, length: 0)
for range: NSRange in pieceRanges {
let item = (str as NSString).substring(with: NSRange(location:previous.location+previous.length, length:range.location-(previous.location+previous.length)))
pieces.append(item)
let piece = (str as NSString).substring(with: range)
pieces.append(piece)
previous = range
}
return pieces
}
}
i have many files which contain similar string which is multiline string for example :
<script> var i = 100
var j = 200
var x = 1000 </script>
and it can be look like this:
<script> var i = 100
var j = 200
var x = 1000 </script>
or
<script> var i = 100
var j = 200
var x = 1000 </script>
and i want to replace it with
<script> var i = 100
var j = 200
var x = xxxx </script>
Notice that the line can be also none spaced and sometimes it can be tabs
The case i have problem is the multiline , if it was simple one line it easir ,
Multi line replacements are easy in perl:
perl -0 -pe 's/<script>\s*var\s+i\s+=\s+100\s+var\s+j\s+=\s+200\s+var\s+x\s+=\s+1000\s+<\/script>/<script> var i =100\n var j =100\n var x = xxxx <\/script>/g' input-file
Or (slightly more readable):
perl -0 -pe 's/<script>\s*
var\s+ i\s+ =\s+ 100\s+
var\s+ j\s+ =\s+ 200\s+
var\s+ x\s+ =\s+1000\s+
<\/script>/<script> var i =100\n var j =100\n var x = xxxx <\/script>/gx' input-file
I want search a div for a string like "12345" and then put every matched string into a span.
But when find repetitive string, just do it for first matched several time.
Here is a jsfiddle:
function find(){
var regex = new RegExp(/12345/g),
list = $(".test").html().match(regex);
console.log(list)
for(each in list){
replacement = $(".test").html().replace(list[each], "<span class='box'>"+list[each]+"</span>");
$(".test").html(replacement);
}
}
find();
.box{
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">
<p>
12345 12345
</p>
</div>
Your approach is faulty: rather than extracting all matching substrings and later iterate them performing single replacements, you may use your own regex inside a String#replace method to modify the substrings "inline", "on-the-match" way:
function find(){
var regex = /12345/g;
var replacement = $(".test").html().replace(regex, "<span class='box'>$&</span>");
$(".test").html(replacement);
}
find();
.box{
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">
<p>
12345 12345
</p>
</div>
My solution (fiddle here), with pure JavaScript :
function find(){
var motif = "12345"
var regex = new RegExp(motif, "g")
document.querySelector("div.test").innerHTML = document.querySelector("div.test").innerHTML.replace(regex, "<span class='box'>" + motif + "</span>")
}
find()
I have a problem when using Regex. I have a html document which create an anchor link when it matches condition.
An example html:
Căn cứ Luật Tổ chức HĐND và UBND ngày 26/11/2003;
Căn cứ Nghị định số 63/2010/NĐ-CP ngày 08/6/2010 của Chính phủ về
kiểm soát thủ tục hành chính;
Căn cứ Quyết định số 165/2011/QĐ-UBND ngày 06/5/2011 của UBND tỉnh
ban hành Quy định kiểm soát thủ tục hành chính trên địa bàn tỉnh;
Căn cứ Quyết định số 278/2011/QĐ-UBND ngày 02/8/2011 của UBND tỉnh
ban hành Quy chế phối hợp thực hiện thống kê, công bố, công khai thủ
tục hành chính và tiếp nhận, xử lý phản ánh, kiến nghị của cá nhân, tổ
chức về quy định hành chính trên địa bàn tỉnh;
Xét đề nghị của Giám đốc Sở Công Thương tại Tờ trình số
304/TTr-SCT ngày 29 tháng 5 năm 2013
I want to match these bold texts and make anchor links from these. If it has, try ignore. Link example 63/2010/NĐ-CP
var matchLegals = new Regex(#"(?:[\d]+\/?)\d+\/[a-z\dA-Z_ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼỀỀỂưăạảấầẩẫậắằẳẵặẹẻẽềềểỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễệỉịọỏốồổỗộớờởỡợụủứừỬỮỰỲỴÝỶỸửữựỳỵỷỹ\-]+", RegexOptions.Compiled);
var doc = new HtmlDocument();
doc.LoadHtml(htmlString);
var allElements = doc.DocumentNode.SelectSingleNode("//div[#class='main-content']").Descendants();
foreach (var node in allElements)
{
var matches = matchLegals.Matches(node.InnerHtml);
foreach (Match m in matches)
{
var k = m.Value;
//dont know what to do
}
}
What can i do this
Many thanks.
I assume your regex pattern is OK and works. Another assumption is that node.InnerHtml doesn't contain any <a> tags already encompassing any of the potential matches.
In this case, it's as simple as doing something like this:
node.InnerHtml = Regex.Replace(node.InnerHtml, "[your pattern here]", "<a href='query=$&'>$&</a>");
...
doc.Save("output.html");
Note, that you may need to work on the href component - I'm unsure how your link should be built.
you match text and replace:
<script>
var s = '...';
var matchs = s.match(/\d{2,3}\/\d{4}\/[a-zA-Z\-áàảãạăâắằấầặẵẫậéèẻẽẹêếềểễệóòỏõọôốồổỗộơớờởỡợíìỉĩịđùúủũụưứửữựÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼÊỀỂỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỨỪỬỮỰỲỴÝỶỸửữựỵỷỹ]+/gi);
if (matchs != null) {
for(var i=0; i<matchs.length;i++){
var val = matchs[i];
s = s.replace(val, '<a href="?key=' + val + '"/>' + val + '</a>');
}
}
document.write(s);
</script>
#Shaamaan thank for your advice. After few hours of coding, it works now
var content = doc.DocumentNode.SelectSingleNode("//div[#class='main-content']");
var items = content.SelectNodes(".//text()[normalize-space(.) != '']");
foreach (HtmlNode node in items)
{
if (!matchLegals.IsMatch(node.InnerText) || node.ParentNode.Name == "a")
{
continue;
}
var texts = node.InnerHtml.Trim();
node.InnerHtml = matchLegals.Replace(texts, a => string.Format("<a href='/search?q={0}'>{0}</a>",a.Value));
}
I want to use regex to get an arrayLike object from the params giving to url
e.g.
http://mysite/myPeople?one=jose&two=emily&three=john
basically what it does is this
function interpretUrl(url){
var reg = /\?([^&?=]*)=([^&?=]*)/g; //what am i doing wrong?
//some logic here
reg.exec(url)
return {
param: [
one: 'jose',
two: 'emily',
three: 'john'
],
root:
}
}
You can use this to get all the parameters from query string:
var re = /([^?&]+)=([^&]*)/g,
matches = {},
input = "http://mysite/myPeople?one=jose&two=emily&three=john";
while (match = re.exec(input.substr(input.indexOf('?')+1))) matches[match[1]] = match[2];
console.log(matches);
//=> {one: "jose", two: "emily", three: "john"}