Skip to content

Commit 5f2de67

Browse files
authored
Img tag regex validator (#2136)
* Create test.js * Delete Specialized Areas/Fix scripts/test.js * Create script.js * Create README.md * Update README.md * Update README.md * Update README.md * Update script.js
1 parent b59f64f commit 5f2de67

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
**Regex Pattern**
2+
1. <img : looks for <img in text
3+
2. \w : looks for any word character (equivalent to [a-zA-Z0-9_])
4+
3. \W : looks for any non-word character (equivalent to [^a-zA-Z0-9_])
5+
4. '>' : looks for character >
6+
7+
**How to use**
8+
1. Run this query in background/Fix scripts.
9+
2. The info message will return articles having images. This is very useful information when there are broken images in articles after movement between instances or tools.
10+
3. This can be further enhanced to replace image src if required.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
This code will search for img tag in kb articles.
3+
Regex Patterns:
4+
<img : looks for <img in text
5+
\w : looks for any word character (equivalent to [a-zA-Z0-9_])
6+
\W looks for any non-word character (equivalent to [^a-zA-Z0-9_])
7+
> : looks for character >
8+
*/
9+
var kbArt = new GlideRecord('kb_knowledge');
10+
kbArt.addEncodedQuery('workflow_state=published'); // encoded get publiushed acticles.
11+
kbArt.query();
12+
while (kbArt.next()) {
13+
var imgRegex = /<img([\w\W]+?)>/; // Regex for checking img tag.
14+
var regex = new RegExp(imgRegex); // forming regex using SN.
15+
if (kbArt.getValue('text') && regex.test(kbArt.getValue('text'))) { // if article body is not empty and has image tag.
16+
gs.info("Image is found in KB Article: " + kbArt.getValue('number'));
17+
}
18+
}

0 commit comments

Comments
 (0)