startsWith()
.طريقة يمكنك تحقق بها إن كان نص يبدء بالعبارة ما و تعيد لك صحيح أو خطأ
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
تركيب الجملة | Syntax
str.startsWith(searchString[, position])
المعاملات | Parameters
searchString- العبارة المبحوث عنها في
النص. positionOptional- مكان الذي يبدأ البحث منه في
النصالإفتراضي 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 |
توافق مع متصفحات
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
startsWith | Chrome Full support 41 | Edge Full support 12 | Firefox Full support 17 | IE No support No | Opera Full support 28 | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support 36 | Firefox Android Full support 17 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs
Full support
4.0.0
|
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.