String.prototype.startsWith()

startsWith()
.طريقة يمكنك تحقق بها إن كان نص يبدء بالعبارة ما و تعيد لك صحيح أو خطأ

تركيب الجملة | Syntax

str.startsWith(searchString[, position])

المعاملات | Parameters

searchString
العبارة المبحوث عنها فيالنص.
position Optional
مكان الذي يبدأ البحث منه فيالنص   الإفتراضي 0

القيمة العائدة | Return value

العائد يكون صحيح إذا وجد تطابق
و إن لم يجيد تطابق يعيد لك خطأ

الوصف | Description

هذه الطريقة حساسة إتجاه الحروف
case-sensitive

أمثلة | Examples

Using startsWith()

//startswith
var str = 'To be, or not to be, that is the question.';

console.log(str.startsWith('To be'));         // true
console.log(str.startsWith('not to be'));     // false
console.log(str.startsWith('not to be', 10)); // true

Polyfill

This method has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet. However, you can polyfill String.prototype.startsWith() with the following snippet:

if (!String.prototype.startsWith) {
    Object.defineProperty(String.prototype, 'startsWith', {
        value: function(search, pos) {
            pos = !pos || pos < 0 ? 0 : +pos;
            return this.substring(pos, pos + search.length) === search;
        }
    });
}

A more robust (fully ES2015 specification compliant), but less performant and compact, Polyfill is available on GitHub by Mathias Bynens.

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'String.prototype.startsWith' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'String.prototype.startsWith' in that specification.
Draft

توافق مع متصفحات

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
startsWithChrome Full support 41Edge Full support 12Firefox Full support 17IE No support NoOpera Full support 28Safari Full support 9WebView Android Full support YesChrome Android Full support 36Firefox Android Full support 17Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 4.0.0
Full support 4.0.0
Full support 0.12
Disabled
Disabled From version 0.12: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
No support  
No support
User must explicitly enable this feature.
User must explicitly enable this feature.

ذات صلة