最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get Instagram post id by using Regex? - Stack Overflow

programmeradmin2浏览0评论

I want to get 11 character post id based on Regex expression. In my code I added two Instagram URL written in different ways, I want a proper Regex expression which gives me 11 characters post id. The Regex expression I am using is not giving proper results.

JsFiddle: /

Code:

<body onload="myFunction()">
</body>

<script>
function myFunction() {
  
var userURL = "/";
var userURL1 = "/";

var regExp = /(https?:\/\/www\.)?instagram\(\/p\/\w+\/?)/;

var match = userURL.match(regExp);
var match1 = userURL.match(regExp);

alert(match);
alert(match1);
  
}</script>

I want to get 11 character post id based on Regex expression. In my code I added two Instagram URL written in different ways, I want a proper Regex expression which gives me 11 characters post id. The Regex expression I am using is not giving proper results.

JsFiddle: https://jsfiddle/z5huq2aL/

Code:

<body onload="myFunction()">
</body>

<script>
function myFunction() {
  
var userURL = "https://www.instagram./p/CBnwdW5n2VA/";
var userURL1 = "https://www.instagram./angelinajolie_offiicial/p/CBnwdW5n2VA/";

var regExp = /(https?:\/\/www\.)?instagram\.(\/p\/\w+\/?)/;

var match = userURL.match(regExp);
var match1 = userURL.match(regExp);

alert(match);
alert(match1);
  
}</script>
Share Improve this question edited Jul 15, 2020 at 10:34 Atomzwieback 6038 silver badges18 bronze badges asked Jul 15, 2020 at 10:31 santosh santosh 8921 gold badge11 silver badges22 bronze badges 1
  • 3 You don't need most of that regex. Just /\/p\/(.*?)\// will do. Then just get match[1] for the sub-pattern (which is the ID you want). – Niet the Dark Absol Commented Jul 15, 2020 at 10:34
Add a ment  | 

2 Answers 2

Reset to default 4

Use a single capturing group and place the /p/ and the trailing / outside of the group.

If you want to match 11 word chars, use \w{11}, else \w+ would suffice.

Assuming the url does not contain whitespace chars, you could use \S*? to match until the first occurrence of /p/

Note that the first part is optional and is not anchored. It matches the http://www. if it is there, but when it is not there, or something else is there, you could still get a partial match for the rest of the pattern.

(?:https?:\/\/www\.)?instagram\.\S*?\/p\/(\w{11})\/?

Regex demo

var userURL = "https://www.instagram./p/CBnwdW5n2VA/";
var userURL1 = "https://www.instagram./angelinajolie_offiicial/p/CBnwdW5n2VA/";

var regExp = /(?:https?:\/\/www\.)?instagram\.\S*?\/p\/(\w{11})\/?/;

var match = userURL.match(regExp);
var match1 = userURL.match(regExp);

console.log(match[1]);
console.log(match1[1]);

Below code should solve your issue:

function myFunction() {


var userURL = "https://www.instagram./p/CBnwdW5n2VA/";
var userURL1 = "https://www.instagram./angelinajolie_offiicial/p/CBnwdW5n2VA/";

var regExp = /\/p\/(.*?)\//;

var match = userURL.match(regExp);
var match1 = userURL.match(regExp);

alert(match[1]);
alert(match1[1]);
  
}

working fiddle here: https://jsfiddle/narkhedetusshar/u2xgh0r5/

发布评论

评论列表(0)

  1. 暂无评论